【发布时间】:2015-02-22 01:39:26
【问题描述】:
最近我在使用 simplexml 时遇到了问题。 我想要做的是获取多次出现的嵌套节点的值。 xml 看起来有点像这样:
<response>
<album id="123">
[...]
<duration>
<value format="seconds">2576</value>
<value format="mm:ss">42:56</value>
<value format="hh:mm:ss">00:42:56</value>
<value format="xs:duration">PT42M56S</value>
</duration>
[...]
</album>
</response>
具体来说,我需要<value format="hh:mm:ss"> 节点的值。
所以我有一个看起来有点像这样的对象的引用:
$this->webservice->album->duration->value;
现在,如果我 var_dump 这个结果将是:
object(SimpleXMLElement)#117 (5) {
["@attributes"]=> array(1) {
["format"]=> string(7) "seconds"
}
[0]=> string(4) "2576"
[1]=> string(5) "42:56"
[2]=> string(8) "00:42:56"
[3]=> string(8) "PT42M56S"
}
我不明白这个输出,因为它采用第一个节点的格式属性(秒)并继续使用数组中的节点值,同时完全忽略以下节点的格式属性。
此外,如果我执行以下操作:
$this->webservice->album->duration->value[2];
结果:
object(SimpleXMLElement)#108 (1) {
["@attributes"]=> array(1) {
["format"]=> string(8) "hh:mm:ss"
}
}
我根本没有要解决的价值。
我也尝试通过以下方式使用 xpath:
$this->webservice->album->duration->xpath('value[@format="hh:mm:ss"]');
导致:
array(1) {
[0]=> object(SimpleXMLElement)#116 (1) {
["@attributes"]=> array(1) {
["format"]=> string(8) "hh:mm:ss"
}
}
}
所以我的问题是: 我究竟做错了什么? xD
提前感谢任何有用的建议:)
【问题讨论】: