views:

2409

answers:

6

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.

+1  A: 

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.

Rob H
Yeah... the main reason I started looking for alternatives is that the "object graph" is actually pretty complex. I'll keep looking... thanks.
fiXedd
A: 

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.

Viktor Bresan
+3  A: 

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.

ng
This library seems nice , i will test it with my android apps, however , there is a tool to build java classes from XSD to this format ?
yeradis
A: 

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.

Dharminder
A: 

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.

hoserdude
A: 

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.

Kevin