GPX parsing patterns and “standards”

久未见 提交于 2019-11-29 05:11:23
Moe

In my applications I'm using a normal XML parser (e.g. pythons xml.dom.minidom) I'm just using the tag names to access the information I need. As it was mentioned in the other Thread, the thing that makes validation fail is the <xsd:sequence> in the GPX standard definition. While it's true that GPX producing devices should respect the standard, it's actually not that big of a deal on the parsing side (at least with the GPX Files/Devices I worked on).

If a GPX file contains additional information then it will be dropped (unless you chose to extract the tagnames unknown to you).

If this method fails because the file is not vaild XML or the GPX uses different tag names for e.g. lat lon information there is no other way to read the file than to write a special importer for this specific use case (I have not seen such files, except for corrupted ones, in the wild)

tony gil

i have just worked out a simple gpx parser that i use to scrape tracks and waypoints from a gpx file and save in a database. i have stripped all DB code and left a barebones example.

<?php
include('simple_html_dom.php');

if (file_exists('5alqs3.gpx')) {
    $xml = simplexml_load_file('5alqs3.gpx');
    foreach( $xml->children() AS $child ) {
        $name = $child->getName();
        if ($name == 'wpt') {
            print_r($name.'    ');
            echo $child['lat'].' '.$child['lon'].'  ';
            $name = $child->children()->getName();
            if ($name = 'ele') {
                echo $child->children().'<br/>';
            }
        }
        if ($name == 'trk') {
            foreach( $child->children() AS $grandchild ) {
                $grandname = $grandchild->getName();
                if ($grandname == 'name') {
                    echo $grandchild.'<br/>';
                }
                if ($grandname == 'trkseg') {
                    foreach( $grandchild->children() AS $greatgrandchild ) {
                        $greatgrandname = $greatgrandchild->getName();
                        print_r($greatgrandname.'  ');
                        //echo '<br/>';
                        if ($greatgrandname == 'trkpt') {
                            echo $greatgrandchild['lat'].' '.$greatgrandchild['lon'];
                            foreach( $greatgrandchild->children() AS $elegreatgrandchild ) {
                                echo $elegreatgrandchild.'<br/>';
                            }
                        }
                        if ($greatgrandname == 'ele') {
                            print_r($greatgrandchild);
                        }   
                    }
                }
            }
        } 
    }
} else {
    exit('Failed to open 5alqs2.gpx.');
}                
?>

you could take this one step further and parse kml from this. check this link out for more on this subject. Creating a kml file from a mysql database with php

THIS CODE WORKS WITH MULTIPLE TRACKS.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!