2012-11-21 33 views
1

xml数据是这样的:让孩子属性使用SimpleXML

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

不起作用。

如何根据属性选择这些孩子?

回答

1

好吧,我现在明白了。对于那些对正确的解决方案感兴趣的人:

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