Re: Parsing GPX with SimpleXML
| From: | Stefan A. | Date: | Wed, 18 Dec 2019 12:07:29 +0000 |
| Subject: | Re: Parsing GPX with SimpleXML | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-326959@lists.php.net to get a copy of this message | ||
This worked for me on PHP 7.2
foreach ($gpx->trk as $trk) {
foreach ($trk->trkseg as $seg) {
foreach ($seg->trkpt as $pt) {
echo "lat: ", $pt["lat"], " lon: ",
$pt["lon"];
echo " ele: ", $pt->ele, " time: ", $pt->time;
echo " extensions: c: ",
$pt->extensions->{'geotracker:meta'}['c'] ?? 'N/A', " s:
",
$pt->extensions->{'geotracker:meta'}['s'] ?? 'N/A',
"\n";
}
}
}
On Wed, Dec 18, 2019 at 1:04 PM Aziz Saleh <azizsaleh@gmail.com> wrote:
>
> On Tue, Dec 17, 2019 at 10:56 PM Ashley M. Kirchner <kirash4@gmail.com>
> wrote:
>
>> Hi folks,
>>
>> I'm having a hard time figuring this one out. I have the following
>> (snippet) of a GPX file:
>>
>> <gpx version="1.1" ...>
>> <trk>
>> <trkseg>
>> <trkpt lat="39.998454" lon="-105.086447">
>> <ele>1568</ele>
>> <time>2019-12-16T10:59:47.000Z</time>
>> <extensions>
>> <geotracker:meta s="0" />
>> </extensions>
>> </trkpt>
>> <trkpt lat="39.998553" lon="-105.086417">
>> <ele>1577</ele>
>> <time>2019-12-16T11:01:39.000Z</time>
>> <extensions>
>> <geotracker:meta c="0.03" s="1.83" />
>> </extensions>
>> </trkpt>
>> ...
>> ...
>> </trkseg>
>> </trk>
>> </gpx>
>>
>> Getting to the most of the nodes is easy:
>>
>> <?php
>> //open gpx file
>> $gpx = simplexml_load_file("Dec_16,_2019_3_59_47_AM.gpx");
>>
>> foreach ($gpx->trk as $trk) {
>> foreach ($trk->trkseg as $seg) {
>> foreach ($seg->trkpt as $pt) {
>> echo "lat: ", $pt["lat"], " lon: ",
>> $pt["lon"];
>> echo " ele: ", $pt->ele, " time: ", $pt->time,
>> "\n";
>> }
>> }
>> }
>> unset($gpx);
>> ?>
>>
>> But for the life of me, I can't figure out how to get information that's
>> on the geotracker:meta line. Also note that not every single one of them
>> will have both the 'c' and 's' attributes. Does anyone have any
>> pointers
>> pls?
>>
>> A
>>
>
> Converting it to array should make it easier for you:
>
>
> https://www.php.net/manual/en/function.xml-parse-into-struct.php
>
>