使用implexml获取子属性 [英] Get children attributes using simplexml
本文介绍了使用implexml获取子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
XML数据如下所示:
<feed>
<entry>
<abc:rank scheme="http://foo.bar">45</abc:rank>
<abc:rank scheme="http://foo2.bar">88</abc:rank>
</entry>
<entry>
<abc:rank scheme="http://foo.bar">125</abc:rank>
<abc:rank scheme="http://foo2.bar">32</abc:rank>
</entry>
</feed>
我可以使用以下代码输出所有这些条目:
foreach($xml->entry[$i]->children('abc', true) as $a) {
echo $a;
}
但是,如果我想要获取第一个条目中内容为"88"的条目,则类似于
foreach($xml->entry[$i]->children('abc', true) as $a) {
if($a["scheme"] == "http://foo2.bar")
echo $a;
}
不起作用。
如何根据属性选择这些子项?
推荐答案
好的,我现在知道了。对于那些对正确解决方案感兴趣的人:
$namespaces = $xml->entry[$i]->getNameSpaces('true');
$abc= $xml->entry[$i]->children($namespaces['abc']);
foreach($abc->rank as $a) {
$scheme = $a->attributes();
echo $scheme['scheme'];
echo " - ";
}
这篇关于使用implexml获取子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文