Using XPath with DOM in a more complicated example
<?
$dom = new DOMDocument;
$dom->load('address-book.xml');
$xpath = new DOMXPath($dom);
$person = $xpath->query('/address-book/person');
foreach ($person as $p) {
$fn = $xpath->query('firstname', $p);
$firstname = $fn->item(0)->firstChild->nodeValue;
$ln = $xpath->query('lastname', $p);
$lastname = $ln->item(0)->firstChild->nodeValue;
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>
Related examples in the same category