更改\ XMLReader的Zend \ Dom \ Document

时间:2015-10-27 13:10:41

标签: php domdocument xmlreader

我需要从使用:Zend \ Dom \ Document和Zend \ Dom \ Document \ Query更改为使用XMLReader,因为解析DOM Document的方式最终会占用太多内存。

这是我需要用XMLReader()替换的行[每行循环?]:

$pages = Query::execute('//page[@id=45894]|//node[@id=45894]|//node[@id=45894]/node|//node[@id=45894]/node/node', $domDocument);

我不确定的是使用一个循环执行相同的XPath查询?

修改 我遇到了hakre的xmlreader-iterators并且现在尝试了:

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.');
}

0 个答案:

没有答案