I'm used to use DOMDocument
and DOMXPath
to parse/traverse through my XML.
Now I have 2,5 MB of XML file and just realized the DOMDocument
is extremely slow in 'handling' that file.
I googled out and found something about XMLReader
which they say it's better way and probably has the fastest performance to handle large XML file
The problem is I don't know how to incorporate the DOMXPath
with the XMLReader
how do I convert this following code to XMLReader
?
$dom = new DOMDocument;
$dom->load('myxml.xml');
$xp = new DOMXPath($dom);
$xp->registerNamespace('f', 'my-name-space:namespace');
$expression = "//f:my-person/@name";
$col = $xp->query($expression);
I've done this but it's still slow:
Note: This is hypothetical code taken from this thread: XML DOMDocument optimization . However, the main point is the same
$xmlReader = new XMLReader();
$xmlReader->open('myxml.xml');
while ($reader->read()) {
switch ($reader->nodeType) {
case (XMLREADER::ELEMENT):
if ($reader->localName == "game") {
$node = $reader->expand();
$dom = new DomDocument();
$n = $dom->importNode($node,true);
$dom->appendChild($n);
$xp = new DOMXPath($dom);
$xp->registerNamespace('f', 'my-name-space:namespace');
$expression = "//f:my-person/@name";
$col = $xp->query($expression);
}
}
}
Feel free to suggest any PHP library to sort this out