Observation #1:
If you have performance problems due to the large size (~35M) of ctlocation.ES.xml
, consider using XmlStreamer for parsing large XML files in PHP.
Observation #2:
Aside from the size of the input XML, there are correctness issues with the PHP you posted.
There is no root level Country
element in ctlocation.ES.xml
, and 'Albania' only exists as a @name
attribute on a /CTLocations/Country
element. Also, you have to account for the namespace in which your input XML elements exist.
Let's use a small sample from the input XML and load it as a string so as to create a small, self-contained example.
To step through the locations of Albania, try this:
<?php
$xmlstring = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<CTLocations xmlns="http://www.cartrawler.com/">
<Country code="AL" name="Albania" continent="Europe">
<Location Id="7188" Name="Tirana Airport" Lat="41.42108838" Lng="19.71271276" CountryCode="AL" Address="Tirana Airport Muhamet Gjollesha Str., Muhamet Gjollesha Str., Tirana" CityName="Tirana" Airport="1" AirportCode="TIA" RailwayStation="0"/>
<Location Id="30768" Name="Tirana Downtown" Lat="41.332" Lng="19.832" CountryCode="AL" Address="Rruga E Durresit. Nr 61, Tirana" CityName="Tirana" Airport="0" RailwayStation="0"/>
<Location Id="52400" Name="Sheraton Italia Square" Lat="0.0" Lng="0.0" CountryCode="AL" Address="Square Italia Hotel Sheraton , Tirana," CityName="Tirana" PostalCode="" Airport="0" RailwayStation="0"/>
</Country>
<Country code="AD" name="Andorra" continent="Europe">
<Location Id="4650" Name="Escaldes" Lat="42.511" Lng="1.548" CountryCode="AD" Address="Avda. Miquel Mateu, 25, Escaldes-engordany , Andorra, Ad700" CityName="Andorra" PostalCode="AD700" Airport="0" RailwayStation="0"/>
<Location Id="152576" Name="Andorra" Lat="42.508" Lng="1.522" CountryCode="AD" Address="Avda D Enclar, 142, Edifici Becier, Andorra La Vella, Ad500" CityName="Andorra La Vella" PostalCode="AD500" Airport="0" RailwayStation="0"/>
</Country>
</CTLocations>
XML;
$xml = simplexml_load_string($xmlstring)
or die("Error: Cannot create object");
$xml->registerXPathNamespace('ct', 'http://www.cartrawler.com/');
echo "The locations in Albania:<br/>";
foreach($xml->xpath("/ct:CTLocations/ct:Country[@name='Albania']/ct:Location") as $loca) {
echo $loca['Name'];
echo "<br/>";
}
echo "Done."
?>
The above script will yield this output:
The locations in Albania:
Tirana Airport
Tirana Downtown
Sheraton Italia Square
Done.