Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Observation #1:</strong></p> <p>If you have performance problems due to the large size (~35M) of <code>ctlocation.ES.xml</code>, consider using <a href="https://github.com/prewk/XmlStreamer" rel="nofollow">XmlStreamer</a> for parsing large XML files in PHP.</p> <p><strong>Observation #2:</strong></p> <p>Aside from the size of the input XML, there are correctness issues with the PHP you posted. There is no root level <code>Country</code> element in <code>ctlocation.ES.xml</code>, and 'Albania' only exists as a <code>@name</code> attribute on a <code>/CTLocations/Country</code> element. Also, you have to account for the namespace in which your input XML elements exist.</p> <p><em>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.</em></p> <p><strong>To step through the locations of Albania, try this:</strong></p> <pre><code>&lt;?php $xmlstring = &lt;&lt;&lt;XML &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;CTLocations xmlns="http://www.cartrawler.com/"&gt; &lt;Country code="AL" name="Albania" continent="Europe"&gt; &lt;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"/&gt; &lt;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"/&gt; &lt;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"/&gt; &lt;/Country&gt; &lt;Country code="AD" name="Andorra" continent="Europe"&gt; &lt;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"/&gt; &lt;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"/&gt; &lt;/Country&gt; &lt;/CTLocations&gt; XML; $xml = simplexml_load_string($xmlstring) or die("Error: Cannot create object"); $xml-&gt;registerXPathNamespace('ct', 'http://www.cartrawler.com/'); echo "The locations in Albania:&lt;br/&gt;"; foreach($xml-&gt;xpath("/ct:CTLocations/ct:Country[@name='Albania']/ct:Location") as $loca) { echo $loca['Name']; echo "&lt;br/&gt;"; } echo "Done." ?&gt; </code></pre> <p><strong>The above script will yield this output:</strong></p> <pre><code>The locations in Albania: Tirana Airport Tirana Downtown Sheraton Italia Square Done. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload