0

我有这个 xml 对象,我想获取每个“p”$xml->query->search["p"][0]->attribute["title"],如何使用 foreach 获取每个对象?

SimpleXMLElement Object
(
    [query] => SimpleXMLElement Object
        (
            [search] => SimpleXMLElement Object
                (
                    [p] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [ns] => 0
                                            [title] => Amebiasis
                                            [timestamp] => 2013-09-16T16:11:24Z
                                        )
                                )
                            [1] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [ns] => 0
                                            [title] => Entamoeba histolytica
                                            [timestamp] => 2013-09-10T19:59:49Z
                                        )
                                )
                        )
                )
        )
)
4

1 回答 1

1

你可以像这样使用xpath

$titles = $o->xpath('query/search/p/@title');

或者获取文档中的所有title属性

$titles = $o->xpath('//@title');
于 2013-09-20T10:28:51.433 回答