简体   繁体   English

将XML文件解组为Android中的Java对象?

[英]Unmarshalling XML files into Java objects in Android?

I'm making use of an API on the internet that is marshalling objects to XML files. 我正在使用互联网上的一个API,它将对象编组到XML文件中。 Given that the XSD files are also available I'd like to be able to unmarshall them back in to Java objects once I've downloaded the files. 鉴于XSD文件也可用,我希望能够在我下载文件后将它们解组回Java对象。

After looking around it looks like JAXB is the default library for doing this in Java, but as I'm developing a mobile app the extra 8.6MB dependency just isn't acceptable. 在环顾四周之后,看起来JAXB是用Java完成此操作的默认库,但在我开发移动应用程序时,额外的8.6MB依赖性是不可接受的。 I also found XStream , but it still weighs in at 7.9MB. 我也发现了XStream ,但它仍然重达7.9MB。

Poking around the Android SDK it looks like the only real XML parser available is SAX . 在Android SDK中,看起来唯一可用的真正XML解析器是SAX

So here's the question: 所以这就是问题:

  1. Is there a way to get SAX to do what I want? 有没有办法让SAX做我想做的事情?
  2. Is there another tool in the Android SDK that I've missed? 我错过了Android SDK中的其他工具吗?
  3. Is there another library (that's significantly smaller) that will do this? 是否会有另一个图书馆(显着更小)?

Thanks. 谢谢。

There is a framework that will work on Android which does Java to XML binding using annotations in a similar manner to JAXB, it is called Simple and weighs in at less than 270K, thats a tiny fraction of whats required for XStream or JAXB. 有一个适用于Android的框架,它使用与JAXB类似的方式使用注释进行Java到XML的绑定,它被称为Simple,重量不到270K,这是XStream或JAXB所需的一小部分。 Also, it has no external dependencies, just one JAR and everything should work. 此外,它没有外部依赖,只有一个JAR,一切都应该工作。 Below is a link to Simple, and XML serialization framework for Java. 下面是Simple的简单链接和Java的XML序列化框架。

http://simple.sourceforge.net http://simple.sourceforge.net

This can also handle cycles in object graphs. 这也可以处理对象图中的循环。

XStream is 431k. XStream是431k。 You don't need the entire distribution zip in your android application. 您不需要Android应用程序中的整个分发zip。 If you add the xpp3 pull parser for faster performance then it would add 24k. 如果你添加xpp3 pull解析器以获得更快的性能,那么它将增加24k。 That would take your entire package to 455k. 这将使你的整个包裹达到455k。 Still not ideal, but I could live with that. 仍然不理想,但我可以忍受。

There are lots of tools to translate objects between Java and XML, but none of those I'm familiar with are any smaller than the ones you found. 有许多工具可以在Java和XML之间转换对象,但是我熟悉的那些工具都不比你找到的那些小。 However, depending on the complexity of your object graph, SAX may be all you need and it has very little overhead. 但是,根据对象图的复杂程度,SAX可能就是您所需要的,而且开销很小。 The trick is to build up the object graph yourself inside the SAX event handlers. 诀窍是在SAX事件处理程序中自己构建对象图。 I've used this technique in a couple of projects before XML marshalers were so widely available, and although it takes a little more work, it is effective. 在XML编组器广泛可用之前,我已经在几个项目中使用了这种技术,虽然它需要更多的工作,但它是有效的。

kxml2-2.2.2 is a nice little lib (43K) which works on JavaME, does pull parsing and I've used it successfully. kxml2-2.2.2是一个很好的小lib(43K),可以在JavaME上工作,拉解析,我已成功使用它。 The only dependency it has is xmlpull, which is 8k. 它唯一的依赖是xmlpull,即8k。 How's that for small? 小的怎么样?

Caveat is that it does not do any binding magic. 警告是它没有做任何绑定魔法。

In Android SDK, besides SAX, DOM parser and XMLPullParser are also available. 在Android SDK中,除了SAX之外,DOM解析器和XMLPullParser也可用。

If you don't want to build in-memory data structure completely by yourself, DOM might ease your pain - as long as the speed and memory footprint are the price you are willing to pay. 如果您不想完全自己构建内存数据结构,DOM可能会减轻您的痛苦 - 只要速度和内存占用是您愿意支付的价格。

We have a product called XBinder that generates classes and encode/decode code from XSD. 我们有一个名为XBinder的产品,可以从XSD生成类和编码/解码代码。 Version 2.1 takes up about 187K. 版本2.1大约需要187K。 Version 2.2 (due out in October 2010) takes about 101K (and is somewhat easier to use with Android than v2.1). 版本2.2(将于2010年10月发布)大约需要101K(并且在Android上比v2.1更容易使用)。 Check it out here . 在这里查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM