如何获取xml元素的值?
我有一些 xml 数据,我正在尝试访问一些元素。数据结构 如下(使用 print_r($data))。 我可以获得 $data->{'parent'}->title
,它可以工作,但是如果我尝试使用获取 href 的值 $data->{'parent'}->link[0]->{'@attributes'}->href
.. 它不起作用.. 有什么想法吗?
谢谢
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
)
)
)
)
)
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?
Thanks
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
)
)
)
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看从 SimpleXML 访问@attribute,尤其是关于SimpleXML 对象的误导性
var_dump
(print_r
) 输出。也就是说,IIRC 以下内容应该在您的示例中起作用:(
也就是说,可以使用标准数组表示法访问属性 - 这绝对适用于单个元素,不确定它是否适用于元素集合中的附加索引。)
Check out Accessing @attribute from SimpleXML, especially the comment on the misleading
var_dump
(print_r
) output of SimpleXML Objects.That said, IIRC the following should work in your example:
(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.)
不要使用 print_r() 来检查 SimpleXMLElement。相反,只需查看 XML。使用对象表示法
->name
访问子项,使用数组表示法['name']
访问属性。在你的情况下,我想访问此属性的正确方法是
Do not use print_r() to inspect a SimpleXMLElement. Instead, just look at the XML. Children are accessed using the object notation
->name
and attributes are accessed using the array notation['name']
.In your case, I guess the correct way to access this attribute would be