Provided a similar xml as follows,
<data>
<train>
<departing>
<schedule>
<time />
<time />
<time />
</schedule>
<locations>
<name>
<name/>
<name>
<name/>
</locations>
</departing>
</train>
<train>
<departing>
<schedule>
<time />
<time />
<time />
</schedule>
<locations>
<name>
<name/>
<name>
<name/>
</locations>
</departing>
</train>
</data>
For simplicity, I have omitted the attributes. The data I'm interested in is the following:
In reality, this xml would be much longer(10+ times). Since this is for android, I want to choose the lightest way to parse this xml. DOM is not my option since it stores too many elements I don't need.
I have been thinking of using SAX. I would use boolean variable to skip unnecessary elements, but I still have to at least reach all of the train elements before I break out of parsing (by throwing exception).
Am I looking at the right approach with SAX for this?
I would first try the XPath option since that will solve your problem with the least amount of code.
I have no knowledge about the internal Android XPath implementation details, but if those turns out to be too heavy weight, I would look at the pull parser packages before submitting to SAX. Pull parsing is usually more straight forward to work with compared to SAX.