1

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" langID="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?

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure what that DOM() class is, and its not working in php5. I thought simpleXML was the best way to work with XML in php?
php.net/dom perhaps it's not enabled on your install, or bundled as a separate package. SimpleXML is ok, but think of it as "DOM-lite"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.