如何获取xml元素的值? [英] How to get values of xml elements?
问题描述
我有一些xml数据,并且正在尝试访问某些元素.数据结构
如下所示(使用print_r($ data)).
我可以得到$data->{'parent'}->title
,它可以工作,但是如果我尝试使用以下方法获得href的值
$data->{'parent'}->link[0]->{'@attributes'}->href
..它不起作用..任何想法吗?
I have some xml data and I am trying to access some elements. The structure of data
is as below (using print_r($data)).
I can get $data->{'parent'}->title
, it works but if I try to get value of href using
$data->{'parent'}->link[0]->{'@attributes'}->href
.. it doesnt work .. any ideas?
谢谢
SimpleXMLElement Object
(
[@attributes] => Array
(
[children] => 29
[modules] => 0
)
[title] => Test title
[link] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[href] => data.php?id=2322
[rel] => self
[type] => application/xml
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[href] => data.php?id=2342
[rel] => alternate
[type] => text/html
)
)
)
[parent] => SimpleXMLElement Object
(
[@attributes] => Array
(
[children] => 6
[modules] => 0
)
[title] => Top
[link] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[href] => /data.php?id=5763
[rel] => self
[type] => application/xml
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[href] => /data.php?id=2342
[rel] => alternate
[type] => text/html
)
)
)
)
)
推荐答案
查看 访问@ 中的属性,尤其是对具有误导性的SimpleXML对象的var_dump
(print_r
)输出的注释.
Check out Accessing @attribute from SimpleXML, especially the comment on the misleading var_dump
(print_r
) output of SimpleXML Objects.
也就是说,IIRC在您的示例中应该可以使用:
That said, IIRC the following should work in your example:
$data->{'parent'}->link[0]['href']
(也就是说,可以使用标准数组表示法来访问属性-这肯定适用于单个元素,不确定是否可以与元素集合中的附加索引一起使用.)
(That is, the attributes can be accessed using standard array notation - this definitely works on single elements, not sure if it works with the additional index into the element collection.)
这篇关于如何获取xml元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!