Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

android - Best practices for parsing XML

My application shall parse XML received via HTTP. As far as I understand there are three major ways of parsing XML:

  • SAX
  • DOM
  • XmlPullParser

It is said that SAX is the fastest of these while DOM is not optimal for larger XML documents. But what is a large XML document in terms of parsing? What would be a recommended parser for the following?

  • XML document size between 1-5 kB
  • Easy traversing through the document, i.e. I need to know not only the current element but also the parent elements.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As far as I understand there are three major ways of parsing XML:
- SAX
- DOM
- XmlPullParser

Wrong! Neither of those is the best way. What you really want is annotation based parsing using the Simple XML Framework. To see why follow this logic:

  1. Java works with objects.
  2. XML can be represented using Java objects. (see JAXB)
  3. Annotations could be used to map that XML to your Java objects and vice versa.
  4. The Simple XML Framework uses Annotations to allow you to map your Java and XML together.
  5. Simple XML is capable of running on Android (unlike JAXB).
  6. You should use Simple XML for all of your XML needs on Android.

And to help you do exactly that I will point you to my own blog post that explains exactly how to use the Simple library on Android.

Unless you have a 100MB XML file then Simple will be more than fast enough for you. It is for me, I use it on all of my Android XML projects.

N.B. I should point out that if you require the user to download XML files that are more than 1MB on Android then you may want to rethink your strategy. You might be doing it wrong.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...