23

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.

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.

Poking around the Android SDK it looks like the only real XML parser available is 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?

Thanks.

6 Answers 6

19

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.

http://simple.sourceforge.net

This can also handle cycles in object graphs.

3
5

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.

2

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.

1
  • Yeah... the main reason I started looking for alternatives is that the "object graph" is actually pretty complex. I'll keep looking... thanks. Aug 30, 2009 at 3:21
1

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?

Caveat is that it does not do any binding magic.

0

In Android SDK, besides SAX, DOM parser and XMLPullParser are also available.

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.