I am trying to parse a XML like this:
<?xml version="1.0" encoding="UTF-8"?>
<gml:FeatureCollection
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:p="http://example.org">
<gml:featureMember>
<p:Point>
<gml:pointProperty>
<gml:Point srsName="epsg:4258">
<gml:pos>-3.84307585 43.46031547</gml:pos>
</gml:Point>
<gml:Point srsName="epsg:4258">
<gml:pos>-3.84299411 43.46018513</gml:pos>
</gml:Point>
<gml:Point srsName="epsg:4258">
<gml:pos>-3.84299935 43.45998723</gml:pos>
</gml:Point>
<!--
... many more <gml:Point> nodes ...
-->
<gml:Point srsName="epsg:4258">
<gml:pos>-3.84309913 43.46054546</gml:pos>
</gml:Point>
<gml:Point srsName="epsg:4258">
<gml:pos>-3.84307585 43.46031547</gml:pos>
</gml:Point>
</gml:pointProperty>
</p:Point>
</gml:featureMember>
</gml:FeatureCollection>
I want to get each of gml:pos rows to save to a DB but for the moment I am happy printing them in webpace (echo...)
$output = simplexml_load_string($output);
$xml = $output->getNamespaces(true);
//print_r( $xml);
$xml_document = $output->children($xml["p"]);
foreach($xml_document->Point->children($xml["gml"]);
echo $xml_point->Point[0];
echo $xml->FeatureCollection;
}
In $output I have the complete xml, tons of coordinates in gml:point
But I am trying to get to the points using namespaces but I have to be doing something wrong because I can't print anything but Array word (even by using print_r...)
getNamespaces(true)), you can just use them directly with->children("p", true). It's better though to define your own array or set of constants with the actual namespace URIs, in case the code generating the XML changes in future and picks different prefixes with the same meaning.