1

i have xml/gpx file:

<?xml version="1.0" encoding="utf-8"?>
<gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1">
  <metadata>
....
  </metadata>
  <trk>
    <trkseg>
      <trkpt lat="50.04551333333333" lon="14.434101666666667">
        <ele>282</ele>
        <time>2014-06-30T20:56:03.92</time>
        <extensions>
          <gpxtpx:TrackPointExtension>
            <gpxtpx:hr>100</gpxtpx:hr>
          </gpxtpx:TrackPointExtension>
        </extensions>
      </trkpt>
...
</gpx>

Load:

$xml=simplexml_load_file($gpx_file);

parse witch

foreach($xml->trk->trkseg->trkpt as $point){}

Thats fine. But i cant get content of /extension//extension/

print_r($point) is

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [lat] => 50.04527
            [lon] => 14.433993333333333
        )

    [ele] => 280.5
    [time] => 2014-06-30T20:57:21.71
    [extensions] => SimpleXMLElement Object
        (
        )
)

and i cannot get SimpleXMLElement Object.

Print_r($point->extension is:

SimpleXMLElement Object
(
)

I try Converting a SimpleXML Object to an Array and other similiar way, but i have failed - output is empty array.

Any idea/way to get into string/array ?

Sample gpx is on www.vovcinec.net/gpx/e.gpx (~700kB)

Community
  • 1
  • 1
tik
  • 47
  • 1
  • 3
  • You XML uses namespaces, see http://stackoverflow.com/a/24101080/2265374 for an example. – ThW Jul 01 '14 at 07:13
  • SimpleXMLElement Object ( [@attributes] => Array ( [lat] => 50.04527 [lon] => 14.433993333333333 ) [ele] => 280.5 [time] => 2014-06-30T20:57:21.71 [extensions] => SimpleXMLElement Object ( ) ) How to get lat and lng?????? please help me – sac Sep 07 '18 at 14:35

2 Answers2

3

You can't get them directly since they are in namespaces, you need to use getNamespaces() method first. Consider this example:

$xml = simplexml_load_file('e.gpx');
foreach($xml->trk->trkseg->trkpt as $trkpt) {

    $namespaces = $trkpt->getNamespaces(true);
    $gpxtpx = $trkpt->extensions->children($namespaces['gpxtpx']);
    $hr = (string) $gpxtpx->TrackPointExtension->hr;
    echo '<pre>';
    print_r($hr);
    echo '</pre>';
}
user1978142
  • 7,946
  • 3
  • 17
  • 20
  • Do not read the namespace from the document using the alias/prefix it is the identifier. The alias/prefix is optional, not unique and can change (even in a single document). – ThW Jul 01 '14 at 07:12
0

I hope you help following code

foreach($gpx->trk->trkseg->children() as $trkpts) {
    echo (string)$trkpts->extensions->children('gpxtpx',true)->TrackPointExtension->hr;
}

no need to use getNamespaces() method

Hitesh Tank
  • 156
  • 1
  • 8