0

I have the following sample XML data which I'd like to parse into SimpleXML with PHP:

<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://ogr.maptools.org/ fire_districts.xsd"
     xmlns:ogr="http://ogr.maptools.org/"
     xmlns:gml="http://www.opengis.net/gml">
  <gml:boundedBy>
    <gml:Box>
      <gml:coord><gml:X>112.921100000165</gml:X><gml:Y>-43.65832660868602</gml:Y></gml:coord>
      <gml:coord><gml:X>153.6407999999201</gml:X><gml:Y>-10.05939999961026</gml:Y></gml:coord>
    </gml:Box>
  </gml:boundedBy>

  <gml:featureMember>
    <ogr:IDM00007 fid="IDM00007.0">
      <ogr:geometryProperty><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>153.551804332204,-28.1645084912738 153.586699999805,-28.2582999997451 153.573299999695,-28.3133 153.582500000375</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
      <ogr:AAC>NSW_FW001</ogr:AAC>
      <ogr:DIST_NO>1</ogr:DIST_NO>
      <ogr:DIST_NAME>Far North Coast</ogr:DIST_NAME>
      <ogr:STATE_CODE>NSW</ogr:STATE_CODE>
      <ogr:SOURCE>RFS</ogr:SOURCE>
      <ogr:GROUP_NAME xsi:nil="true"/>
    </ogr:IDM00007>
  </gml:featureMember>
  <gml:featureMember>
    <ogr:IDM00007 fid="IDM00007.1">
      <ogr:geometryProperty><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>153.261225211474,-29.9401032948311 153.19820000033,-30.038899999935 153.20060000039,-30.1036999997551 153.214099999715</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
      <ogr:AAC>NSW_FW002</ogr:AAC>
      <ogr:DIST_NO>2</ogr:DIST_NO>
      <ogr:DIST_NAME>North Coast</ogr:DIST_NAME>
      <ogr:STATE_CODE>NSW</ogr:STATE_CODE>
      <ogr:SOURCE>RFS</ogr:SOURCE>
      <ogr:GROUP_NAME xsi:nil="true"/>
    </ogr:IDM00007>
  </gml:featureMember>
</ogr:FeatureCollection>

I've tried a few different methods of loading this XML using SimpleXML however whenever I do a print_r of the data it's just blank:

<?php
$data = file_get_contents("fire_districts.gml");
$xml = new SimpleXMLElement($data);
$xml->registerXPathNamespace('f', 'http://ogr.maptools.org/');
$xml->registerXPathNamespace('g', 'http://www.opengis.net/gml');
$gml = $xml->xpath('/f:FeatureCollection/g:featureMember');
print_r($gml);
?>

The output is as follows:

Array
(
    [0] => SimpleXMLElement Object
        (
        )

    [1] => SimpleXMLElement Object
        (
        )

    [2] => SimpleXMLElement Object
        (
        )

    [3] => SimpleXMLElement Object
        (
        )
....

What do I need to change to successfully load this GML data into an array?

Cheers, Mike

1 Answers1

0

You can try by printing out the contents of the '$data' variable using 'var_dump' or 'echo' to see if it contains the expected data.

else

Try simplexml_load_string() instead of file_get_contents().

Harun
  • 111
  • 1
  • 1
  • 5
  • Hi Harun, I tried doing a var_dump however it still had an empty array. The simplexml_load_string() also had no luck with an empty array. I got a feeling it's a namespace issue but just can't seem to work it out. – Mike Manning Feb 23 '23 at 22:45
  • I ended up doing a str_replace: $data = str_replace("ogr:", "gml:", $data); That got rid of the 2nd Namespace and is working now – Mike Manning Feb 23 '23 at 23:27