Search code examples
androidxmlxml-parsingandroid-xmlsax

Is SAX appropriate for this xml parsing in Android?


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:

  • Attributes in every train element.
  • Elements within a particular train element.

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?


Solution

  • 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.