I receive an XML repsonce from a GEO service (PDOK). The $responce->raw_body contains this XML structure:
<xls:GeocodeResponse xmlns:xls="http://www.opengis.net/xls" xmlns:gml="http://www.opengis.net/gml">
<xls:GeocodeResponseList numberOfGeocodedAddresses="1">
<xls:GeocodedAddress>
<gml:Point srsName="EPSG:28992">
<gml:pos dimension="2">121299.73296672151 487003.8972524117</gml:pos>
</gml:Point>
<xls:Address countryCode="NL">
<xls:StreetAddress>
<xls:Street>Rokin</xls:Street>
</xls:StreetAddress>
<xls:Place type="MunicipalitySubdivision">Amsterdam</xls:Place>
<xls:Place type="Municipality">Amsterdam</xls:Place>
<xls:Place type="CountrySubdivision">Noord-Holland</xls:Place>
</xls:Address>
</xls:GeocodedAddress>
</xls:GeocodeResponseList>
</xls:GeocodeResponse>
How can I access the elements in here. For example I want an PHP array to access the element 121299.73296672151 487003.8972524117 to grap the coordinates.
And also the other elements. I used SimpleXML parser but I recieve always null. I think it has someting to do with the namespaces. But I have no clue how to solve this.
The responce is coming from:
$url = "http://geodata.nationaalgeoregister.nl/geocoder/Geocoder?zoekterm=xxxxx%20xx";
$response = \Httpful\Request::get($url)
->expectsXml()
->send();
$xml = new \SimpleXMLElement($response->raw_body);
print_r( $xml);
output:
SimpleXMLElement Object ( )
Any help is appriciated!
After some digging I found a solution for my problem. Its indeed by the namespaces and when using xpath after the namespace registration you can find the elements you need.
$xml = new \SimpleXMLElement($response);
$xml->registerXPathNamespace('xls', 'http://www.opengis.net/xls');
$xml->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
foreach($xml->xpath('//xls:GeocodeResponseList') as $header)
{
$geocoordinates = $header->xpath('//gml:pos');
$street = (string) ($header->xpath('//xls:Street')[0]);
$place = (string) ($header->xpath('//xls:Place')[2]);
}
echo $geocoordinates[0]; // get the coordinates needs to split on space
echo "<br />";
echo $geocoordinates[0]['dimension']; // get the dimension attribute
echo "<br />";
echo $street;
echo "<br />";
echo $place;