Finding a node in an XML document by its attribute value
I am trying to grab a value from a xml feed, but I am not sure how to get it. Feed:
<name>
<namerecord nameID="0" platformID="1" platEncID="0" langID="0x0">
Hello World
</namerecord>
<namerecord nameID="1" platformID="1" platEncID="0" langID="0x0">
MyName
</namerecord>
<namerecord nameID="6" platformID="1" platEncID="0" langID="0x0">
Another Record
</namerecord>
<namerecord nameID="12" platformID="1" platEncID="0" 开发者_如何学PythonlangID="0x0">
Another Record Again
</namerecord>
I am trying to grab the items from that, by target what is the value of nameID. If I target it just by like namerecord[0] its not correct in the way I need it.
I have tried numerous things like:
$test = $xml->name->namerecord->attributes('nameId, '12');
Any suggestions?
That's now how you handle XML:
$dom = new DOM();
$dom->load('your xml here');
$xp = new XPath($dom);
$node = $xp->query('//namerecord[@nameID=12]')->item(0);
$nameID = $node->getAttribute('nameID');
精彩评论