Best practices for parsing XML

后端 未结 2 718
长情又很酷
长情又很酷 2020-12-10 00:25

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

  • SAX
  • DOM
  • XmlPullParser
2条回答
  •  無奈伤痛
    2020-12-10 00:53

    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.

提交回复
热议问题