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