I need to change from using: Zend\Dom\Document and Zend\Dom\Document\Query to using XMLReader, as parsing the DOM Document way ends up using too much memory.
This is the line I need to replace with XMLReader() [looping per line?]:
$pages = Query::execute('//page[@id=45894]|//node[@id=45894]|//node[@id=45894]/node|//node[@id=45894]/node/node', $domDocument);
What I'm unsure about is doing the same XPath query with one loop ?
EDIT: I came across hakre's xmlreader-iterators and have now tried:
if (get_class($reader) === Document::class) {
/** @var Document $reader */
$nodes = Query::execute($queryXpath, $reader);
} elseif (get_class($reader) === XMLReaderInterface::class) {
/** @var XMLReaderInterface $reader */
$nodes = null;
$xmlElementIterator = new \XMLElementIterator($reader);
$nodes = new \XMLElementXpathFilter($xmlElementIterator, $queryXpath);
$doc = new \DOMDocument();
$doc->createElement('root');
foreach ($nodes as $node) {
/* @var $node \XMLReaderNode */
$element = $node->getSimpleXMLElement();
$domElement = dom_import_simplexml($element);
$doc->appendChild($doc->importNode($domElement));
}
$nodes = $doc->childNodes;
$reader->reset();
} else {
throw new \Exception('Invalid reader type, must use for example: Document or XMLReader.');
}