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


No comments:

Post a Comment