This article is linked to the GPX parsing library that I published - fork it here on Github.
For a recent Android, project I needed to parse GPX files. ‘GPX’ is an acronym for GPS Exchange Format and it is an XML format to describe routes, tracks and waypoints, together with information about altitude, timing and so forth. This is an example:
When faced with the need to parse such files in Android, and map them to convenient structures, I found no existing library to do it. Therefore I set out to make one myself. The first step was to map all these GPX object in a convenient way, so for instance:
What took me the most time was the actual XML parsing. My last encounter with XML was a while ago, so I had to wrap my head around it again. I decided to use the default XmlPullParser shipped with the Android SDK. You start by defining the XML tags in Java:
And then it becomes a matter of recursively scanning the GPX file, “nesting” one level deeper when you encounter specific tags, and parsing each tag attribute. This is for instance how you can parse a TrackPoint:
The library I published is largely incomplete - it only parses the elements I needed to parse - but it can be expanded to be more complete. All contributions to expand this library are most welcome, so fork it at will!