XML Parser: How to parse a gpx file?

Toni

Member
Licensed User
Longtime User
Hello together,
I can't figure out how to parse a gpx file.

Example:

B4X:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="www.gps-tour.info" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
  <metadata>
    <name>Rundwanderung Ratingen-Schwarzbach</name>
    <author>
      <name>Herbert Backhaus</name>
    </author>
    <link href="m.gps-tour.info/de/touren/detail.92930.html">
      <text>Rundwanderung Ratingen-Schwarzbach on www.gps-tour.info</text>
    </link>
    <time>2012-09-16T08:42:01Z</time>
    <bounds minlat="51.2701594" minlon="6.8771381" maxlat="51.284978" maxlon="6.9318763"/>
  </metadata>
  <wpt lat="51.2812461" lon="6.9167127">
    <ele>134</ele>
    <name>Bank mit netter Aussicht</name>
    <sym>Waypoint</sym>
  </wpt>
  <wpt lat="51.2852097" lon="6.8775487">
    <ele>68</ele>
    <name>Start und Ziel</name>
    <sym>Waypoint</sym>
  </wpt>
  <trk>
    <name>Rundwanderung Ratingen-Schwarzbach</name>
    <trkseg>
      <trkpt lat="51.284978" lon="6.8772785">
         <ele>73.2</ele>
         <time>2012-09-16T08:42:01Z</time>
      </trkpt>
      <trkpt lat="51.2848808" lon="6.8771381">
         <ele>71.3</ele>
         <time>2012-09-16T08:42:15Z</time>
      </trkpt>
                <trkpt lat="51.2846029" lon="6.877505">
         <ele>69.8</ele>
         <time>2012-09-16T08:42:54Z</time>
      </trkpt>
      
      ...
      
   </trkseg>
  </trk>
</gpx>

What I think that i've understood is that e.g. lat and lon are attributes,
but ele and time are names. Attributes has to be catched by Parser_StartElement and Names by Parser_EndElement?!

my code so far:

B4X:
Sub LoadFile_Click
   Dim fd As FileDialog
   fd.FastScroll = True
   fd.FilePath = ""   
   fd.FileFilter = ".gpx" 
   ret = fd.Show("Choose a .gpx file", "Yes", "No", "", Null)   
   If ret=-1 Then
      Dim FileToStr As String 
      FileToStr=File.ReadString(fd.FilePath, fd.ChosenName)      
      Msgbox(FileToStr,"")                       'just to see what I'm doing
   Else
      Activity.Finish
   End If   
   'parse the xml file
   Dim In As InputStream
   In = File.OpenInput(fd.FilePath, fd.ChosenName)
   parser.Parse(In, "Parser")
   In.Close
End Sub

Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
    If Name="trkpt"  Then
       lati = Attributes.GetValue2("","lat") 
       Log(lati) 
      loni = Attributes.GetValue2("","lon") 
      Log(loni)
    End If
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If parser.Parents.IndexOf("trkpt") > -1 Then
        If Name = "ele" Then
            elevation = Text.ToString
         Log(elevation)
        Else If Name = "time" Then
            timestamp = Text.ToString
         Log (timestamp)
        End If
    End If
    If Name = "trkpt" Then
        Log(elevation) 
      Log(timestamp) 
    End If
End Sub

The answer of the compiler:

Compiling code. 0.02
Compiling layouts code. 0.00
Generating R file. 0.22
Compiling generated Java code. Error
B4A line: 64
lati = Attributes.GetValue2(\
javac 1.6.0_26
src\b4a\example\main.java:482: inconvertible types
found : java.lang.String
required: org.xml.sax.Attributes
mostCurrent._lati.setObject((org.xml.sax.Attributes)(_attributes.GetValue2("","lat")));
^
1 error

I've declared lati and loni as attributes, that seems to be completly wrong. But how to?
And 2nd question: Can i ignore e.g. trkseg because it is only in a few gpx files?

I think, I didn't understand anything how the parser works.
Thank you for your attention!

Sorry for my English but I'm not used to!
 
Last edited:

Toni

Member
Licensed User
Longtime User
Where are lati and loni declared?

Under Public Variables as attributes. String also not works for me.
But i can't look right now to my code, cause I'am in my office.

Have you seen the XML tutorial?

Yes but there is just an example for the end_element.
Because, if I delete the parsing code for the start_element, my code for the end_element (elevation and time) is also not working (no error but also no output in the log).
I think that i did not understand the concept of the parser and what is the difference between an attribute and a node in a xml file:sign0013:

Thank you for your help!
 
Upvote 0

Toni

Member
Licensed User
Longtime User
Just a suggestion - you might find my XOM library an easier way to parse your GPX file.

Thank you very much for your help Martin!

I'll have a look to your library, but can't see the advantages right now at a glance. But i'll take me time therefore!
 
Upvote 0

Toni

Member
Licensed User
Longtime User
For those people, got the same problem like me:
I rewrote from scratch and that is my (working) solution:

B4X:
Sub Process_Globals
   Dim parser As SaxParser
End Sub

Sub Globals
   Dim LoadFile As Button
   Dim lati As String
   Dim loni As String
   Dim elevation As String
   Dim timestamp As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("CreateRoutingMain")  'with a button to call Loadfile
   If FirstTime Then
      parser.Initialize
   End If
End Sub

Sub LoadFile_Click
   Dim fd As FileDialog
   fd.FastScroll = True
   fd.FilePath = ""   
   fd.FileFilter = ".gpx" 
   ret = fd.Show("Choose a .gpx file", "Yes", "No", "", Null)   
   If ret=-1 Then
      Dim FileToStr As String 
      FileToStr=File.ReadString(fd.FilePath, fd.ChosenName)      
      Msgbox(FileToStr,"")                       'just to see what is included in the file
   Else
      Activity.Finish
   End If   
   'parse the xml file
   Dim In As InputStream
   In = File.OpenInput(fd.FilePath, fd.ChosenName)
   parser.Parse(In, "Parser")
   In.Close
End Sub

Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
    If Name="trkpt"  Then
       lati = Attributes.GetValue2("","lat") 
       Log(lati) 
      loni = Attributes.GetValue2("","lon") 
      Log(loni)
    End If
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If parser.Parents.IndexOf("trkpt") > -1 Then
        If Name = "ele" Then
            elevation = Text.ToString
         Log(elevation)
        Else If Name = "time" Then
            timestamp = Text.ToString
         Log (timestamp)
   Else
        End If
    End If
End Sub
 
Upvote 0

postasat

Active Member
Licensed User
Longtime User
Hi,
I tried to parse a little gpx file with the above code but at the end of the file reading, I have the following error.

Error occurred on line: 0 (importa_gpx2)
org.apache.harmony.xml.ExpatParser$ParseException: At line 62, column 11: no element found

What can I do ?

Thank you,
Roberto.
 
Upvote 0
Top