Using XPath with SimpleXML

$s = simplexml_load_file('addresses.xml');
$people = $s->xpath('/addresses/person');
 
foreach($people as $p) {
    list($firstname) = $p->xpath('firstname');
    list($lastname) = $p->xpath('lastname');
 
    print "$firstname $lastname\n";
}

Input XML:

<?xml version="1.0"?>
<addresses>
    <person>
        <name>Ramesh babu</name>
	<firstname>Ramesh</firstname>
	<lastname>babu</lastname>
        <email>Ram@w3mauthor.com</email>
    </person>
    <person>
        <name>Jolld babu</name>
	<firstname>jolld</firstname>
	<lastname>babu</lastname>
        <email>Jolld@w3mauthor</email>
    </person>
</addresses>

Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

Filed Under: PHP XML Examples

RSSComments (0)

Trackback URL

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.