How can XML nodes be filtered and reduced for efficient searching in PHP without loading the entire file into memory?

When dealing with large XML files in PHP, loading the entire file into memory can be inefficient and resource-intensive. To address this, XML nodes can be filtered and reduced using XPath queries to only load and process the necessary parts of the XML document. This allows for more efficient searching and manipulation of XML data without the need to load the entire file into memory.

$xml = new SimpleXMLElement('data.xml', null, true);

// Use XPath query to filter and reduce nodes for efficient searching
$nodes = $xml->xpath('//book[author="John Doe"]');

foreach ($nodes as $node) {
    // Process each filtered node as needed
    echo $node->title . "\n";
}