解析XML最佳实践 [英] Best practices for parsing XML
问题描述
我的申请应通过解析HTTP接收XML。据我了解有解析XML的三种主要方式:
- SAX
- DOM
- XmlPullParser
据说SAX是最快的这些而DOM是不是最佳的较大的XML文档。但是,什么是在分析方面有很大的XML文档?什么将是下一个推荐的解析器?
- 在XML文档1-5 KB 的大小
- 易于遍历整个文档,也就是我需要知道的不仅是当前的元素也是父元素。
据我了解有解析XML的三种主要方式:
- SAX
- DOM
- XmlPullParser
错!这些都不是最好的方式。你真正想要的是使用href="http://simple.sourceforge.net/" rel="nofollow">简单的XML框架的
和帮你做的正是我会的点,你到我的博客帖子这正好说明了如何使用Android上的简单的库。 除非你有一个100MB的XML文件,然后简单的将超过足够快为您服务。这是对我来说,我用它在我所有的Android XML的项目。 N.B。我要指出的是,如果你需要用户下载XML文件超过1MB在Android上,那么你可能要重新考虑你的策略。你可能会做是错误的。 My application shall parse XML received via HTTP. As far as I understand there are three major ways of parsing XML: 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?
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:
- Java works with objects.
- XML can be represented using Java objects. (see JAXB)
- Annotations could be used to map that XML to your Java objects and vice versa.
- The Simple XML Framework uses Annotations to allow you to map your Java and XML together.
- Simple XML is capable of running on Android (unlike JAXB).
- 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.
这篇关于解析XML最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!