开发者

Finding a node in an XML document by its attribute value

开发者 https://www.devze.com 2023-03-08 10:39 出处:网络
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\">

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');
0

精彩评论

暂无评论...
验证码 换一张
取 消