Best practices for parsing XML

后端 未结 2 727
长情又很酷
长情又很酷 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:28

    I'm afraid this is a case of, it depends ...

    As a rule of thumb, using Java to build a DOM tree from an XML document will consume between 4 and 10 times that document's native size (assuming Western text and UTF-8 encoding), depending on the underlying implementation. So if speed and memory-use are not critical it will not be a problem for the small documents you mention.

    DOM is generally regarded as quite an unpleasant way to work with XML. For background you might want to look at Elliotte Rusty Harold's presentation: What's Wrong with XML APIs (and how to fix them).

    However, using SAX can be even more tedious as the document is processed one item at a time. SAX however is fast and consumes very little memory. If you can find a pull parser you like then by all means try that.

    Another approach (not super-efficient, but clean and maintainable) is to build an in-memory tree of your XML (using DOM, say) and then use XPath expressions to select the information you are interested in.

提交回复
热议问题