Unmarshalling XML files into Java objects in Android?

android, java, xml, xsd

Solution

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.

Problem

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: - Is there a way to get SAX to do what I want? - Is there another tool in the Android SDK that I've missed? - Is there another library (that's significantly smaller) that will do this? Thanks.

Original source