問題描述
我正在使用 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 對象?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!