如何从SimpleXMLElement对象中检索值

时间:2014-03-27 16:48:26

标签: php object simplexml

在调用prestashop webservice之后,我收到了响应,如下所示:

SimpleXMLElement Object
(
    [order] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 1
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 2
                        )

                )

        )

)

我尝试通过循环来检索内容,如下所示:

foreach($resources as $resource){
    echo '<pre>';
    print_r($resource['id']);
    echo '</pre>';
}

这给了我:

SimpleXMLElement Object
(
    [0] => 1
)
SimpleXMLElement Object
(
    [0] => 2
)

如何检索1和2这些对象的值?感谢

1 个答案:

答案 0 :(得分:1)

我讨厌simplexml ......

<?php
$xml = file_get_contents('xml.xml');
$xml = new SimpleXMLElement($xml);

foreach($xml as $key => $value)
{
        $attrs = $value->attributes();
        foreach($attrs as $attr_k => $attr_v)
                echo $attr_k.": ".$attr_v."\n";
}