Unmarshalling XML files into Java objects in Android?(在 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.
那么问题来了:
- 有没有办法让 SAX 做我想做的事?
- 我错过了 Android SDK 中的另一个工具吗?
- 是否有另一个库(要小得多)可以做到这一点?
谢谢.
推荐答案
有很多工具可以在 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 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Android 中将 XML 文件解组为 Java 对象?
基础教程推荐
- 大摇大摆的枚举 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
