Jump to Navigation

009 - Searching and Filtering with XPath

The standard way to search through XML documents for particular nodes is called XPath. XPath is much easier than regular expressions for basic usage.

Example:

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

echo "<strong>Using direct method....</strong><br />";
$name = $xml->xpath('/employees/employee/name');
foreach($names as $name) {
echo "Found $name<br />";
}
echo "<br />";

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

echo "<strong>Using direct method....</strong><br />";
$name = $xml->xpath('//name');
foreach($names as $name) {
echo "Found $name<br />";
}
echo "<br />";

 

PHP:


Main menu 2

Story | by Dr. Radut