File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Best XML parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Best XML parser" Watch "Best XML parser" New topic
Author

Best XML parser

john sayeau
Ranch Hand

Joined: Aug 23, 2010
Posts: 33
Hi. I have a Garmin device that logs my run workouts. You can get an XML file using their software. I'm trying to figure out how to pull the data out of the XML file that looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/ActivityExtension/v2 http://www.garmin.com/xmlschemas/ActivityExtensionv2.xsd http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">

<Folders/>

<Activities>
<Activity Sport="Running">
<Id>2010-09-27T19:12:45Z</Id>
<Lap StartTime="2010-09-27T19:12:45Z">
<TotalTimeSeconds>3109.3400000</TotalTimeSeconds>
<DistanceMeters>4534.0097656</DistanceMeters>
<MaximumSpeed>2.3450601</MaximumSpeed>
<Calories>393</Calories>
<AverageHeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
<Value>148</Value>
</AverageHeartRateBpm>
<MaximumHeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
<Value>176</Value>
</MaximumHeartRateBpm>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
<Track>
<Trackpoint>
<Time>2010-09-27T19:12:45Z</Time>
<Position>
<LatitudeDegrees>51.1039107</LatitudeDegrees>
<LongitudeDegrees>-114.0948912</LongitudeDegrees>
</Position>
<AltitudeMeters>1175.3488770</AltitudeMeters>
<DistanceMeters>0.0000000</DistanceMeters>
<HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
<Value>97</Value>
</HeartRateBpm>
<SensorState>101</SensorState>
</Trackpoint>

//and so on for a long ways.



I'd like to pull out the data for some of the elements stick the data into an array and then do stuff (graphs, mapping a route)
I've been looking at Dom and Xom but they have examples that are too simple. My question is what would be the best parser to use and a pointer to a good tutorial.

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

For beginners the best parser is the one built into Java. Don't waste your time downloading other parsers and figuring out how to make your code use them, just get going with a parser you already have.

I'm not going to suggest any tutorials. Apparently you have looked at some and rejected them. I don't know which ones those were so I can't suggest something else. But "too simple" is a wrong criterion. You do need to start with something simple before going on to more complex tasks.
john sayeau
Ranch Hand

Joined: Aug 23, 2010
Posts: 33
Maybe too simple is the wrong term. There are examples with code and an XML file. The problem with the ones I've found so far it's kind of hard to understand what the code is doing. I've used one of the examples to successfully fish out the data I need using dom, but I don't totally understand what It's doing. I guess I need to read the api docs and brush up on my xml terms.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Can you be more specific as to what issues you're having understanding the tutorials? It's a fixed-format and shouldn't be particularly difficult to parse (I've done it, in fact, in three languages; I <3 my Garmins).
john sayeau
Ranch Hand

Joined: Aug 23, 2010
Posts: 33
Hi David,
I understand what's going on now. Now that I know how to fish the data out I can go an learn how to use it to do some graph drawing, mapping etc..
This little project is going to be a personal workout tracker. I'm just going to use the garmin training center to get the xml file for now.
In the future I want to figure out how to talk to the Garmin directly to get the data. I have a Garmin forerunner 305 and a edge 305 (bike). Good learning project.
I was thinking about doing this in Cocoa/Obj-c as well.
I used DOM...here's the code:

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Glad you worked it put :)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Yeah, DOM code is kind of obscure. And even when you have it right, it's still ugly code. I try to avoid it as much as possible, but it's still good training for beginners, just to get the idea of how an XML document is structured.

Personally I prefer to apply XSLT, rather than Java, to my Garmin files. But that's a whole other language so I'm not going to make that a recommendation. Not yet anyway.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Best XML parser