Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmarshalling XML files into Java objects in Android?

Tags:

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.

like image 756
Jeremy Logan Avatar asked Aug 29 '09 11:08

Jeremy Logan


People also ask

What is Unmarshalling in Java?

The Unmarshaller class governs the process of deserializing XML data into newly created Java content trees, optionally validating the XML data as it is unmarshalled. It provides an overloading of unmarshal methods for many different input kinds.

Can you use XML with Java?

XML provides a universal syntax for Java semantics (behavior). Simply put, this means that a developer can create descriptions for different types of data to make the data behave in various ways with Java programming code, and can later repeatedly use and modify those descriptions.

What is XML and Java in Android Studio?

Android uses xml to declare layouts and java to provide logic. Note that while both activity_main and MainActivity follow common naming conventions, there is no need for them to be called this way.


1 Answers

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.

like image 160
ng. Avatar answered Oct 03 '22 23:10

ng.