0

What is the fastest way to parse & query a large XML file in PHP? I have a file arround 200 mb. And i am trying to parse & query a certain value when another information from the same tree is given. I am using the below code to do that:

$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);
        $result = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$hotelCodes[0].'"]/HotelImages/ImageURL[1])', $node);
        $dom->removeChild($node);
        if ($result) {
            echo $result;

        }
    }
}

XML Structure is:

<?xml version="1.0" encoding="UTF-8" ?> 
- <XMLResponse>
    <ResponseType>HotelListResponse</ResponseType> 
  - <RequestInfo>
      <AffiliateCode>XX0000</AffiliateCode> 
      <AffRequestId>XXX</AffRequestId> 
      <AffRequestTime>yyyy-mm-ddThh:mm:ss</AffRequestTime> 
    </RequestInfo>
    <TotalNumber>XXXXX</TotalNumber> 
  - <Hotels>
    - <Hotel>
        <HotelCode>XXXXXX</HotelCode>
        <OldHotelId>X/XXXXXXXX</OldHotelId> 
        <DestinationId>XXXX</DestinationId> 
        <Destination>Name of city</Destination> 
        <Country>Name of country</Country> 
        <HotelName>Hotel Name</HotelName> 
        <StarRating>x</StarRating> 
        <HotelAddress>Hotel address</HotelAddress>
        <HotelPostalCode>Zip Code</HotelPostalCode>
        <HotelPhoneNumber>Hotel phone number</HotelPhoneNumber> 
        <HotelArea>Hotel area</HotelArea>
        <Chain>Hotel Chain</Chain> 
      - <Coordinates>
          <Latitude>latitude</Latitude> 
          <Longitude>longitude</Longitude> 
        </Coordinates>
      - <HotelImages>
          <ImageURL>URL1</ImageURL> 
          <ImageURL>URL2</ImageURL> 
          <ImageURL>URL3</ImageURL> 
        </HotelImages>
      </Hotel>
  - <Hotel>
    ....
    </Hotel>
  ....
  </Hotels>
</XMLResponse>

It is working, but it takes a lot to retrieve the data in my site.

Is there any faster solution to do what i need? or a method to convert the XML file into a SQL file.?

Thanks

Razvan George
  • 115
  • 1
  • 11
  • Have you tried `SimpleXMLElement`? Not sure if it can work good with large files. I never worked too much with XMLs and when I did, they were small :P – Forien Jan 05 '15 at 08:08
  • SimpleXML is not good solution for large files it consumes alot of mem. Dom and XPath manipulations are time consuming ones. – Ziumin Jan 05 '15 at 08:14
  • I'd opt for not using the xml document as the working model but as an import format that is then stored to (and queried from) a "standard" database. Your xpath expression looks like something a relational database should be good at.... – VolkerK Jan 05 '15 at 08:27
  • possible duplicate of this http://stackoverflow.com/questions/3048583/what-is-the-fastest-xml-parser-in-php – Aram Tchekrekjian Jan 05 '15 at 09:07

0 Answers0