【问题标题】:Access array returned from xpath in PHP从 PHP 中的 xpath 返回的访问数组
【发布时间】:2014-05-08 12:25:53
【问题描述】:

如何访问(以显示)xpath 返回的元素?

我的 xpath 调用是

$products = $this->_dom->xpath('//menu/category[@name="'.$category.'"]');

我得到的是

    Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => pizza ) [item] 
=> SimpleXMLElement Object ( [@attributes] => Array ( [name] => Tomato and Cheese ) [type] => 
Regular [available] => true [size] => Array ( [0] => SimpleXMLElement Object ( [@attributes] 
=> Array ( [name] => Small ) [price] => 5.50 ) [1] => SimpleXMLElement Object ( [@attributes] 
=> Array ( [name] => Large ) [price] => 9.75 ) ) ) ) [1] => SimpleXMLElement Object ( 
[@attributes] => Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( [@attributes] 
=> Array ( [name] => Pepperoni ) [type] => Regular [available] => true [size] => Array ( [0] 
=> SimpleXMLElement Object ( [@attributes] => Array ( [name] => Small ) [price] => 6.85 ) [1] 
=> SimpleXMLElement Object ( [@attributes] => Array ( [name] => Large ) [price] => 10.85 ) ) ) 
) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => pizza ) [item] => 
SimpleXMLElement Object ( [@attributes] => Array ( [name] => Meatball ) [type] => Regular 
[available] => true [size] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array 
( [name] => Small ) [price] => 6.85 ) [1] => SimpleXMLElement Object ( [@attributes] => Array 
( [name] => Large ) [price] => 10.85 ) ) ) ) [3] => SimpleXMLElement Object ( [@attributes] => 
Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( [@attributes] => Array ( [name] 
=> Hawaiian ) [type] => Regular [available] => true [size] => Array ( [0] => SimpleXMLElement 
Object ( [@attributes] => Array ( [name] => Small ) [price] => 7.95 ) [1] => SimpleXMLElement 
Object ( [@attributes] => Array ( [name] => Large ) [price] => 11.80 ) ) ) ) [4] => 
SimpleXMLElement Object ( [@attributes] => Array ( [name] => pizza ) [item] => 
SimpleXMLElement Object ( [@attributes] => Array ( [name] => Three Aces Special ) [type] => 
Speciality [available] => true [size] => Array ( [0] => SimpleXMLElement Object ( 
[@attributes] => Array ( [name] => Small ) [price] => 9.80 ) [1] => SimpleXMLElement Object ( 
[@attributes] => Array ( [name] => Large ) [price] => 15.80 ) ) ) ) [5] => SimpleXMLElement 
Object ( [@attributes] => Array ( [name] => pizza ) [item] => SimpleXMLElement Object ( 
[@attributes] => Array ( [name] => Mediterranean ) [type] => Speciality [available] => true 
[size] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Small ) 
[price] => 9.80 ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Large ) 
[price] => 15.80 ) ) ) ) )

我想做的是以列表的形式显示这些信息,所以我想知道如何访问 SimpleXMLElement 对象的元素。

谢谢

【问题讨论】:

    标签: php xml xpath


    【解决方案1】:

    清理输出:

    [0] => SimpleXMLElement Object ( 
        [@attributes] => Array ( [name] => pizza )
        [item] => SimpleXMLElement Object ( 
            [@attributes] => Array ( [name] => Tomato and Cheese ) 
            [type] => Regular 
            [available] => true 
            [size] => Array ( 
                [0] => SimpleXMLElement Object ( 
                    [@attributes] => Array ([name] => Small ) 
                    [price] => 5.50 ) 
                [1] => SimpleXMLElement Object (
                    [@attributes] => Array ([name] => Large ) 
                    [price] => 9.75 ) 
            ) 
        )
    ) 
    

    这样的东西会起作用,虽然你的很多数据都被包装成属性,这会让它变得有点冗长,比如 size 属性。您可能需要转换项目才能获得价值。

    $products = $this->_dom->xpath('//menu/category[@name="'.$category.'"]');
    
    foreach ($products as $product) {
        // accessing SimpleXML values is tricky
        // $name is a SimpleXMLElement Object
        $name = $product['title']; 
    
        // $name2 has the string value of the title attribute
        $name2 = (string) $product['title']; 
    
        // following works because __toString is implemented
        // by SimpleXMLElement class  
        echo "Title: " . $product['title']; 
    
        // alternative syntax, unless you cast to a 
        // string, you have a SimpleXML Object  
        $name3 =  $product->attributes()->title;
    }
    

    Access @attributes data in SimpleXMLElement in PHP

    【讨论】:

    • 嘿,谢谢你的回答,我之前试过了,但是没有,结果是空白的。还有什么想法吗?谢谢
    • 也许你必须转换值。我不知道这是否仅适用于较旧版本的 SimpleXML。
    猜你喜欢
    • 2011-02-21
    • 2010-11-30
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多