phpxmlgml

PHP : Parse an XML File with GML tags


I need some help ! I'm triying to parse an XML file with GML tags inside using php. I'm using the simplexml_load_file function to parse my file and it works well except for the gml: tags.

Here is the file :

<par>
  .....
  <gml:Polygon>
    <gml:outerBoundaryIs>
      <gml:LinearRing>
        <gml:coordinates>
          {{ DATAS I WOULD LIKE TO PARSE}}
        </gml:coordinates>
      </gml:LinearRing>
    </gml:outerBoundaryIs>
  </gml:Polygon>
 ......
</par>

And here is the code i'm using :

    <?php
if (file_exists('doc.xml')) {
    $xml = simplexml_load_file('doc.xml');
} 
...    
foreach ($xml->...->par->{'gml:Polygon'}->{'gml:outerBoundaryIs'}->{'gml:LinearRing'}->{'gml:coordinates'} as $coords) {
        echo $coords;
        echo '<br>';   
    }

Running that code, I get that :

Warning: Attempt to read property "gml:LinearRing" on null in \www\xmlphptest\index.php on line 44

Warning: Attempt to read property "gml:coordinates" on null in \www\xmlphptest\index.php on line 44

Warning: foreach() argument must be of type array|object, null given in \www\xmlphptest\index.php on line 44

Can you please help me ! THANKS :)


Solution

  • Found it on the php documentation. Was related to xml with namespaces : https://www.php.net/manual/fr/simplexmlelement.registerxpathnamespace.php

    Thanks