解析XML最佳实践 [英] Best practices for parsing XML

查看:165
本文介绍了解析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框架的

  1. 在Java的工作与对象。
  2. 使用Java对象
  3. 在XML可以重新presented。 (看到JAXB
  4. 注释可用于将XML映射到Java对象,反之亦然。
  5. 在简单的XML框架使用注解,让您可以将Java和XML映射到一起。
  6. 在简单的XML能够在Android(不像JAXB)运行。
  7. 您应该使用简单的XML 所有Android上的XML的需求。

和帮你做的正是我会的点,你到我的博客帖子这正好说明了如何使用Android上的简单的库。

除非你有一个100MB的XML文件,然后简单的将超过足够快为您服务。这是对我来说,我用它在我所有的Andr​​oid 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:

  • 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.

解决方案

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.

这篇关于解析XML最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆