1

I am using the below code in PHP to parse a XML file and retrieve a certain information from the file.

$dom    = new DOMDocument();
$xpath  = new DOMXPath($dom);
$reader = new XMLReader();
$reader->open('File.xml');
while ($reader->read()) {
    if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'Hotel') {
        $node = $dom->importNode($reader->expand(), true);
        $dom->appendChild($node);
        $h1name = $xpath->evaluate('string(/Hotel[HotelCode = "'.$hotelCodes[0].'"]/HotelName)', $node);
        $dom->removeChild($node);
        if ($h1name) {
         $reader->close();
         break;
        }
    }
}

I am using a VPS with CENTOS x64 , SSD drive, 1GB of ram and a 1.8 ghz CPU. The code works but is very slow. If i parse a 20 mb file it takes ~1 minute fo the page to load.

Could you please help to improve the code so i can get a faster working solution ?

Razvan Baba
  • 155
  • 9
  • I think it is a duplicate question here is the answer http://stackoverflow.com/questions/1167062/best-way-to-process-large-xml-in-php – Brainstroming Jan 13 '15 at 08:17
  • possible duplicate of [PHP - very basic XMLReader](http://stackoverflow.com/questions/23072961/php-very-basic-xmlreader) – ThW Jan 13 '15 at 10:01
  • Actually your current source is not bad, if you check my answer in the linked you will see an optimization. Use `$reader->next('Hotel');` to go directly to the next Hotel node. – ThW Jan 13 '15 at 10:05
  • @ThW where should $reader->next('Hotel'); be fit after $node = $dom->importNode($reader->expand(), true); ? – Razvan Baba Jan 13 '15 at 10:48

0 Answers0