在 Android 中将 XML 文件解组为 Java 对象? [英] Unmarshalling XML files into Java objects in Android?

查看:24
本文介绍了在 Android 中将 XML 文件解组为 Java 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

环顾四周,看起来 JAXB 是在 Java 中执行此操作的默认库,但正如我在开发移动应用程序时,额外的 8.6MB 依赖是不可接受的.我还找到了 XStream,但它的大小仍然为 7.9MB.

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. I also found XStream, but it still weighs in at 7.9MB.

在 Android SDK 周围一探究竟,似乎唯一可用的真正 XML 解析器是 SAX.

Poking around the Android SDK it looks like the only real XML parser available is SAX.

那么问题来了:

  1. 有没有办法让 SAX 做我想做的事?
  2. 我错过了 Android SDK 中的另一个工具吗?
  3. 是否有另一个库(要小得多)可以做到这一点?

谢谢.

推荐答案

有很多工具可以在 Java 和 XML 之间转换对象,但我熟悉的工具没有一个比你找到的那些更小.但是,根据对象图的复杂性,SAX 可能就是您所需要的,而且开销很小.诀窍是在 SAX 事件处理程序中自己构建对象图.在 XML 编组器如此广泛可用之前,我已经在几个项目中使用过这种技术,虽然它需要更多的工作,但它很有效.

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.

这篇关于在 Android 中将 XML 文件解组为 Java 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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