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 ?

4
  • I think it is a duplicate question here is the answer stackoverflow.com/questions/1167062/… Commented Jan 13, 2015 at 8:17
  • possible duplicate of PHP - very basic XMLReader Commented Jan 13, 2015 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. Commented Jan 13, 2015 at 10:05
  • @ThW where should $reader->next('Hotel'); be fit after $node = $dom->importNode($reader->expand(), true); ? Commented Jan 13, 2015 at 10:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.