DAJ DAJ - 3 months ago 8
PHP Question

How do I access specific values in my xml?

Let's assume I have simple XML, such as:

<people>
<person id="52">
<name>John</name>
</person>
<person id="53">
<name>Sally</name>
</person>
</people>


I use
$xml=simplexml_load_file('filename')
to load the file.

How do I reference the name of person with id 53, without iterating through looking for the person?

Answer

Use xpath() to search for the matching element.

$person = $xml->xpath("/people/person[@id='53']")[0];
Comments