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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1351218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:31:12  来源:igfitidea点击:

Unmarshalling XML files into Java objects in Android?

javaxmlandroidxsd

提问by Jeremy Logan

I'm making use of an API on the internet that is marshalling objects to XML files. 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.

我正在使用 Internet 上的 API 将对象编组为 XML 文件。鉴于 XSD 文件也可用,我希望能够在下载文件后将它们解组回 Java 对象。

After looking around it looks like JAXBis 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. I also found XStream, but it still weighs in at 7.9MB.

环顾四周后,看起来JAXB是在 Java 中执行此操作的默认库,但是当我正在开发移动应用程序时,额外的 8.6MB 依赖项是不可接受的。我还找到了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?
  2. Is there another tool in the Android SDK that I've missed?
  3. Is there another library (that's significantly smaller) that will do this?
  1. 有没有办法让 SAX 做我想做的事?
  2. 我错过了 Android SDK 中的另一个工具吗?
  3. 是否有另一个图书馆(明显更小)可以做到这一点?

Thanks.

谢谢。

采纳答案by Rob H

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. However, depending on the complexity of your object graph, SAX may be all you need and it has very little overhead. The trick is to build up the object graph yourself inside the SAX event handlers. 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.

有很多工具可以在 Java 和 XML 之间转换对象,但我所熟悉的工具都比您发现的要小。但是,根据对象图的复杂性,SAX 可能就是您所需要的,而且它的开销很小。诀窍是在 SAX 事件处理程序中自己构建对象图。在 XML 封送拆收器如此广泛可用之前,我已经在几个项目中使用了这种技术,虽然它需要做更多的工作,但它是有效的。

回答by Viktor Bre?an

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 可能会减轻您的痛苦——只要速度和内存占用是您愿意付出的代价。

回答by ng.

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. Also, it has no external dependencies, just one JAR and everything should work. Below is a link to Simple, and XML serialization framework for Java.

有一个框架可以在 Android 上运行,它使用注释以类似于 JAXB 的方式进行 Java 到 XML 绑定,它被称为 Simple 并且重量小于 270K,这是 XStream 或 JAXB 所需的一小部分。此外,它没有外部依赖项,只有一个 JAR,一切都应该可以正常工作。下面是 Java 的 Simple 和 XML 序列化框架的链接。

http://simple.sourceforge.net

http://simple.sourceforge.net

This can also handle cycles in object graphs.

这也可以处理对象图中的循环。

回答by Dharminder

XStream is 431k. You don't need the entire distribution zip in your android application. If you add the xpp3 pull parser for faster performance then it would add 24k. That would take your entire package to 455k. Still not ideal, but I could live with that.

XStream 为 431k。您的 android 应用程序中不需要整个分发 zip。如果您添加 xpp3 pull 解析器以获得更快的性能,那么它将增加 24k。这将使您的整个包裹达到 455k。仍然不理想,但我可以忍受。

回答by hoserdude

kxml2-2.2.2 is a nice little lib (43K) which works on JavaME, does pull parsing and I've used it successfully. The only dependency it has is xmlpull, which is 8k. How's that for small?

kxml2-2.2.2 是一个不错的小库(43K),它在 JavaME 上工作,可以拉解析,我已经成功地使用了它。它唯一的依赖项是 xmlpull,它是 8k。小的怎么样?

Caveat is that it does not do any binding magic.

需要注意的是,它不会做任何绑定魔法。

回答by Kevin

We have a product called XBinder that generates classes and encode/decode code from XSD. Version 2.1 takes up about 187K. Version 2.2 (due out in October 2010) takes about 101K (and is somewhat easier to use with Android than v2.1). Check it out here.

我们有一个名为 XBinder 的产品,它可以从 XSD 生成类和编码/解码代码。2.1 版占用大约 187K。2.2 版(将于 2010 年 10 月发布)大约需要 101K(与 v2.1 相比,Android 更容易使用)。检查它在这里