PHP: XML Parsing with simplexml_load_file()

  • PHP’s SimpleXML extension was introduced back in PHP 5.0
  • An increasing number of web services return data in JSON format, but a large number still return XML
  • The code below demonstrates  both reading of : XML Elements and XML Attributes
  • The entire DOM tree is read into memory and returns a SimpleXMLElement PHP object
  • Use this APi with caution for large XML files 

XML data

    <trk>
        <name>Neue Positionsliste 1</name>
        <trkseg>
            <trkpt lat="49.81208" lon="11.353953">
                <ele>379.0</ele>
                <time>2015-12-07T12:47:05.000Z</time>
                <name>Position 1</name>
                <extensions>
                    <nmea:speed>2.45</nmea:speed>
                </extensions>
            </trkpt>
            <trkpt lat="49.812103" lon="11.353962">
                <ele>379.0</ele>
                <time>2015-12-07T12:47:06.000Z</time>
                <name>Position 2</name>
                <extensions>
                    <nmea:speed>2.75</nmea:speed>
                </extensions>
            </trkpt>
      ...

PHP code

 function parseGPX($dirName, $fileName)
  {
  global $gpxArray;
  global $gpxCount;
  
  $gpxArray = simplexml_load_file($dirName.$fileName);
  $gpxCount = count($gpxArray->trk->trkseg->trkpt);
  $i = 0;
  print_r($gpxArray->trk->trkseg->trkpt[0]);
  print_r("First GPX Element:: Lat: ".$gpxArray->trk->trkseg->trkpt[0]["lat"]." - Lon: ".$gpxArray->trk->trkseg->trkpt[0]["lon"]. 
           " - Ele: ".$gpxArray->trk->trkseg->trkpt[0]->ele."\n");
  printf("\n");   
  foreach ($gpxArray->trk->trkseg->trkpt as $gpxElem) 
      {
      $i++;
      if ( $i == 1 || $i == $gpxCount || ( $i % 100) === 0 )
        printf("\n% -4d: Lat: % -10s - Lon:% -10s - Ele: % -6s", $i, $gpxElem["lat"], $gpxElem["lon"], $gpxElem->ele );   
    }    
  printf("\n");   
  print_r("GPX Array Count: ".count($gpxArray->trk->trkseg->trkpt)."\n");
  error_log('gpxCount: '.$gpxCount);
  }

PHP Output

(
    [@attributes] => Array
        (
            [lat] => 49.81208
            [lon] => 11.353953
        )

    [ele] => 379.0
    [time] => 2015-12-07T12:47:05.000Z
    [name] => Position 1
    [extensions] => SimpleXMLElement Object
        (
        )

)
Output:   
First GPX Element:: Lat: 49.81208 - Lon: 11.353953 - Ele: 379.0

1   : Lat: 49.81208   - Lon:11.353953  - Ele: 379.0
100 : Lat: 49.81406   - Lon:11.353427  - Ele: 393.0
200 : Lat: 49.814999  - Lon:11.35364   - Ele: 405.0
300 : Lat: 49.815887  - Lon:11.354074  - Ele: 420.0
400 : Lat: 49.816792  - Lon:11.35475   - Ele: 409.0
500 : Lat: 49.817665  - Lon:11.355126  - Ele: 407.0
600 : Lat: 49.818573  - Lon:11.354907  - Ele: 414.0
700 : Lat: 49.819462  - Lon:11.35405   - Ele: 417.0
800 : Lat: 49.820202  - Lon:11.353247  - Ele: 417.0
900 : Lat: 49.820946  - Lon:11.35268   - Ele: 427.0
1000: Lat: 49.821693  - Lon:11.352285  - Ele: 438.0
1100: Lat: 49.822273  - Lon:11.353273  - Ele: 441.0
1200: Lat: 49.823071  - Lon:11.353742  - Ele: 448.0
1222: Lat: 49.823299  - Lon:11.353806  - Ele: 448.0
GPX Array Count: 1222  

Reference

Leave a Reply

Your email address will not be published. Required fields are marked *