Searching and Filtering with XPath : xpath « XML « PHP

Searching and Filtering with XPath

    $xml = simplexml_load_file('employees.xml');

    $names = $xml->xpath('/employees/employee/name');
    foreach($names as $name) {
            echo "Found $name<br />";
    echo "<br />";

    $employees = $xml->xpath('/employees/employee');
    foreach($employees as $employee) {
            echo "Found {$employee->name}<br />";

    $names = $xml->xpath('//name');
    foreach($names as $name) {
            echo "Found $name<br />";

Related examples in the same category

1.Combine it with an XPath search
3.Using XPath and DOM
4.Using XPath and SimpleXML in a basic example
5.Using XPath with DOM in a basic example
6.Using XPath with SimpleXML in a more complicated example
7.XPath DOM Query
8.XPath can be used to filter your results according to any values