0

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is good advice, I implemented a working solution to my problem using DOM but I think that this is better than my result so i'll try to use this, many thanks.
1

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.

Comments

0

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 :)

1 Comment

You could for example write your own interpreter using regex (PCRE library, which should be enabled by default). php.net/manual/en/ref.pcre.php You could also just parse the entire text yourself by just going through each character and detecting patterns.

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.