Luke h
Luke h

Reputation: 337

Retrieve XML element data with PHP

i'm trying to allow the user to retrieve a specific XML node and print the information. My XML data is as follows:

   <people>
    <person id="1">
     <forename>Jim</forename>
     <surname>Morrison</surname>
    </person>
    <person id="2">
     <forename>James</forename>
     <surname>Frank</surname>
    </person>
   </people>

I want to be able to search my XML document with a name or ID, for instance I want to be able to say, 'check that james exists, and if james does, print out his information, otherwise print out an error message'. I've figured that I first need to include the file with:

    $xml=simplexml_load_file("credentials.xml") or die("Error: Cannot create object");

From this point onwards i'm unsure how to proceed, i've looked at SimpleXML and XPATH but i'm unsure on how to use these to acheive this. Thanks if you can help

Upvotes: 0

Views: 126

Answers (3)

michi
michi

Reputation: 6625

Let's xpath():

$xml = simplexml_load_string($x); // assume XML in $x
$result = $xml->xpath("/people/person[forename = 'James']")[0];

The above xpath-expression will select any <person> having 'James' as a <forename> and store it as an array in $result.

With the [0] at the end of line 2, we select only the first entry in that array and store it in $result. This requires PHP >= 5.4.

We could also write to get the same result:

$result = $xml->xpath("/people/person[forename = 'James']");
$result = $result[0];

If there were more than 1 James in the XML, we would only get the first. To get all Jameses, do as in line 1 above.

Then, let's output the <person> selected by our xpath expression:

echo $result->forename . ' ' . $result->surname .' has id ' . $result['id'] . ".";

In case $result contains several Jameses, do:

foreach ($result as $person) {
    echo $person->forename . ' ' . $person->surname .' has id ' . $person['id'] . "." . PHP_EOL;
}

see it working: https://eval.in/222195

Upvotes: 1

Horse SMith
Horse SMith

Reputation: 1035

You should use the manual.

simplexml_load_file returns a SimpleXMLElement object.

From here you can use the objects children method to get the children you want, which would also be more of that SimpleXMLElement objects. Let's say you first want the children called people, and then want person. When you got the person objects, you can get the value of the attribute by the method attributes to get the names and the values etc. Because SimpleXMLElement implements Traversable, you can use a foreach loop to loop through the lists/arrays you get in return.

Upvotes: 1

Luke h
Luke h

Reputation: 337

Figured out how to mostly solve the problem with this:

    $xml=simplexml_load_file("credentials.xml") or die("Error: Cannot create object");
    foreach($xml->children() as $people){ 
     if ($people->forename == "james" ){echo $people->forename;}
    }

This may be inefficient so if anyone else has a better way please do specify :)

Upvotes: 0

Related Questions