Way to parse XML (org.w3c.Document) on Android

后端 未结 3 1641
醉梦人生
醉梦人生 2020-12-05 06:13

Can anyone point me to a explanation for or explain to me how I can easily parse the XML and get values of a w3c.Document on Android using only Android OS Libs?

I tr

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 06:43

    You should definitively use the SAX APIs provided. Android XML parse API stacks on top of the normal java SAX one so in my opinion I would just use the normal Java SAX API and this way you get the ability to test your code as normal java desktop project.

    XML parsing is always slow and using these SAX parsers is as good as it gets (unless you write a custom parser from the scratch). Word of advice: Try to minimize string comparison and try to use hashmaps instead of long chains of if (token.isEqual(CONSTANT_TOKEN));.

    Here are a few examples of Java XML parsing: http://java.sun.com/developer/codesamples/xml.html#sax

    Android XML API is declarative, the paradigm is a bit different so in case you decide going with that, be prepared to read a few examples to learn its ways.

    Finally, maybe 2 months ago I saw a chart benchmarking DOM vs SAX vs Android XML API (I can't quite find the link now). The conclusion was that DOM was by far the slowest and SAX was top but not by a huge margin over Android's XML implementation.

提交回复
热议问题