SimpleXMLElement php,foreach

时间:2013-09-20 10:03:20

标签: php xml

我有这个xml对象,我想得到每个“p”$ xml-> query-> search [“p”] [0] - >属性[“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
                                        )
                                )
                        )
                )
        )
)

1 个答案:

答案 0 :(得分:1)

你可以像这样使用xpath

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

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

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