search

Home  >  Q&A  >  body text

SimpleXML not loading GML data

I have the following sample XML data that I want to parse into SimpleXML using 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 ways of loading this XML using SimpleXML, but whenever I do a print_r on the data, it just goes 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

P粉718165540P粉718165540232 days ago481

reply all(1)I'll reply

  • P粉025632437

    P粉0256324372024-04-05 09:05:49

    You can try to use "var_dump" to print the contents of the "$data" variable or 'echo' to see if it contains the expected data.

    other

    Try using simplexml_load_string() instead of file_get_contents().

    reply
    0
  • Cancelreply