Wednesday, 31 December 2014

XML / PHP - Extracting information from multiple document nodes



I was wondering if someone could help me out with some code.


I have a KML ( Google Earth XML ) which im trying to import into my application.


The problem im having is that when extracting the data, it works perfect up until the first node thing and wont process the rest of the file.


The KML file is massive so i will show a sample below:



<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://ift.tt/Wr35qg" xmlns:gx="http://ift.tt/1agnx6F" xmlns:kml="http://ift.tt/Wr35qg" xmlns:atom="http://ift.tt/r5ut6F">
<Folder>
<name>Kalamunda</name>
<open>1</open>
<Document>
<name>Export 1418786594.kml</name>
<Placemark>
<name>76-122 Tourist Drive 207</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.072723865509,-31.98064436184923,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>76-122 Tourist Drive 207</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0731288790703,-31.98067621355563,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>76-122 Tourist Drive 207</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0735687613487,-31.98064436184923,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>12 Hinkler Rd</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0740676522255,-31.98083319680418,0</coordinates>
</Point>
</Placemark>
</Document>
<Document>
<name>Export 1418785221.kml</name>
<Placemark>
<name>LOT 435 Collins Rd</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0624188184738,-31.97887202447751,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>25 Central Rd</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0589158535004,-31.9744125903374,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>49 Canning Rd</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0574942827225,-31.97629422494352,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>13 Anne Ave</name>
<description>Walliston WA 6076</description>
<Point>
<coordinates>116.0673272609711,-31.99400063250859,0</coordinates>
</Point>
</Placemark>
</Document>
<Document>
<name>Export 1418783501.kml</name>
<Placemark>
<name>3 Lookout Rd</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.037417948246,-31.98043960061557,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>9 Ozone Terrace</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0382279753685,-31.98028261669366,0</coordinates>
</Point>
</Placemark>
</Document>
</Folder>
</kml>


The code im using is as follows:



$i = 0;
foreach( $xml_data->Folder->Document->Placemark as $placemark ) {

$data[$i][] = $placemark->name;
$data[$i][] = $placemark->description;

list($x, $y) = explode(',', $placemark->Point->coordinates);
$data[$i][] = array($x, $y);

$i++;

}


Using this code, it will extract the data up until the 12 Hinkler Rd placemark, I think because there is a closing tag, it wont go any further.


How would i go about extracting all the data given an XML in this format?


Any help would be greatly appreciated.


Cheers,


No comments:

Post a Comment