Get children attributes using simplexml

2020-04-20 08:10发布

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?

标签: simplexml
1条回答
戒情不戒烟
2楼-- · 2020-04-20 08:38

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 " - ";
    }
查看更多
登录 后发表回答