Get children attributes using simplexml

倖福魔咒の 提交于 2019-12-02 07:37:00

问题


The xml data looks like this:

<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>

I am able to output all of these entries with this code:

foreach($xml->entry[$i]->children('abc', true) as $a) {
    echo $a;
}

However, if I want to get the one with the content "88" in the first entry, something like

foreach($xml->entry[$i]->children('abc', true) as $a) {
    if($a["scheme"] == "http://foo2.bar")
        echo $a;
}

does not work.

How can I select these children depending on their attribute?


回答1:


Ok I got it by now. For those interested in the correct solution:

    $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 " - ";
    }


来源:https://stackoverflow.com/questions/13495892/get-children-attributes-using-simplexml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!