How to foreach simplexml, SimpleXMLElement Object, xml file using php
I am trying to go through an xml file. I first loaded the XML file as an SimpleXMLElement object. I am trying to go through each value. How do I go through the values using foreach()?
When I run the print_r it gives me:
SimpleXMLElement Object ( [PERSON] => SimpleXMLElement Object ( [NAME] => Jake [CITY] => Tempe [AGE] => 22 ) )
So, I would like to go through using foreach() and get the name, city and age of the person.
I attached the code for the xml file and the php file
Thank you!
PHP FILE:<html><head></head><body><?php $xml = simplexml_load_file('peoples.xml'); print_r($xml); echo "<br />"; //I know this is way off foreach($xml->PEOPLE as $key1){ foreach($key1 as $key => $value){ echo $key." = ".$value."<br />"; } } ?></body></html>XML FILE:<?xml version='1.0' encoding='ISO-8859-1'?><PEOPLE> <PERSON> <NAME>Jake</NAME> <CITY>Tempe</CITY> <AGE>22</AGE> </PERSON> <PERSON> <NAME>Rick</NAME> <CITY>New York</CITY> <AGE>34</AGE> </PERSON> <PERSON> <NAME>Dan</NAME> <CITY>San Jose</CITY> <AGE>19</AGE> </PERSON></PEOPLE>
$xml = simplexml_load_file('peoples.xml');$result = $xml->xpath('/people/person');foreach($result as $person) {// make some action hereprint_r($person);}
http://de.php.net/manual/en/function.simplexml-element-xpath.php
Open in new window