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

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

问题描述

我正在使用已编组对象到XML文件在互联网上的一个API。鉴于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.

因此​​,这里的问题:

So here's the question:

  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.

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

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