Mega Code Archive

 
Categories / Php / XML
 

Using XPath with SimpleXML in a more complicated example

<? $s = simplexml_load_file('address-book.xml'); $people = $s->xpath('/address-book/person'); foreach($people as $p) {     list($firstname) = $p->xpath('firstname');     list($lastname) = $p->xpath('lastname');          print "$firstname $lastname\n"; } ?> // <?xml version="1.0"?> <address-book>     <person id="1">         <firstname>D</firstname>         <lastname>S</lastname>         <city>New York</city>         <state>NY</state>         <email>s@php.net</email>     </person>     <person id="2">         <firstname>A</firstname>         <lastname>T</lastname>         <city>San Francisco</city>         <state>CA</state>         <email>a@php.net</email>     </person> </address-book>