Take the 2-minute tour ×

I have a 5MB xml file

I'm using the following code to get all nodeValue

$dom        =   new DomDocument('1.0', 'UTF-8');

$games = $dom->getElementsByTagName("game");
foreach($games as $game)


This takes 76 seconds and there are around 2000 games tag. Is there any optimization or other solution to get the data?

share|improve this question
I can't imagine optimizing a loop without knowing what the loop does. –  Herbert Sep 4 '11 at 15:15
look this link [enter link description here][1] [1]: stackoverflow.com/questions/188414/best-xml-parser-for-php –  steve Sep 4 '11 at 15:15
@steve: maybe you can elaborate and put that in the form of an answer. How can SimpleXML speed up the loop to get at the data? –  Herbert Sep 4 '11 at 15:18
You can find some useful suggestion in this [link][1] [1]: stackoverflow.com/questions/188414/best-xml-parser-for-php –  monish Sep 4 '11 at 15:19
The loop is empty... –  run Sep 4 '11 at 15:19
show 1 more comment

2 Answers

up vote 0 down vote accepted

You shouldn't use the Document Object Model on large XML files, it is intended for human readable documents, not big datasets!

If you want fast access you should use XMLReader or SimpleXML.

XMLReader is ideal for parsing whole documents, and SimpleXML has a nice XPath function for retreiving data quickly.

For XMLReader you can use the following code:


// Parsing a large document with XMLReader with Expand - DOM/DOMXpath 
$reader = new XMLReader();


while ($reader->read()) {
    switch ($reader->nodeType) {
        case (XMLREADER::ELEMENT):
        if ($reader->localName == "game") {
             $node = $reader->expand();
             $dom = new DomDocument();
             $n = $dom->importNode($node,true);
             $xp = new DomXpath($dom);
             $res = $xp->query("/game/title"); // this is an example
             echo $res->item(0)->nodeValue;

The above will output all game titles (assuming you have /game/title XML structure).

For SimpleXML you can use:

$xml = file_get_contents($url);
$sxml = new SimpleXML($xml);
$games = $sxml->xpath('/game'); // returns an array of SXML nodes
foreach ($games as $game)
   print $game->nodeValue;
share|improve this answer
Thanks for your help. I have two questions what is the slash before the games. and How can I get the string in this element: object(SimpleXMLElement)[8991] string 'Handball' (length=8), I want the handball –  run Sep 4 '11 at 17:30
No probs... The slash in /game shows the root of the document. This is how XPath works (Google XPath for more info). In order to answer your second question I would need to see an example of the XML you are using. If you edit your question and paste it in, I can see it. –  AlexW Sep 4 '11 at 21:16
SimpleXML also loads the whole file, which brings absolutely no speed improvements. DOM itself has XPath support, too. –  cweiske Sep 5 '11 at 9:15
@cweiske - Did you not notice I suggested XMLReader first? This is faster. To OP: Please read through these pages for everything you need to know about PHP and XML ibm.com/developerworks/xml/library/x-xmlphp1/index.html –  AlexW Sep 5 '11 at 13:22
add comment

I once wrote a blog article about loading huge XML files with XMLReader - you probably can use some of it.

Using DOM or SimpleXML is no option, since both load the whole document into memory.

share|improve this answer
SimpleXml is quite good, I tested on an xml file, the DOM took around 30sec and the SimpleXML took 1sec:) –  run Sep 5 '11 at 11:01
SimpleXML has proved perfectly useful for OP and DOM is too slow - exactly as I suggested. XMLReader is the fastest along with SAX. –  AlexW Sep 5 '11 at 13:23
add comment

Your Answer


