Especially if you want to transfer objects from and to a Android mobile device, XML serialization may be an option. XStream is a popular XML serialization tool, however it does not work out-of-the-box with Google Android. Although serialization works, the current XStream 1.2.2 has problems deserializing it: it throws an com.thoughtworks.xstream.converters.ConversionException caused by a java.lang.NullPointerException. Fortunately, this was easy to fix and in the course of patching XStream I stripped XStream down a bit. By removing classes that do not work on Android anyway, I could reduce it by about 20%.

Donwload patched XStream 1.2.2 for Android (Java Source)

21 Responses to “Android: XML Serialization with XStream”
  1. NTR says:

    Thanks a lot, this may prove to be useful. I guess I can use your modified version freely for my devious needs?

  2. Markus Junginger says:

    Yes, it’s still BSD license – have a look at the license.txt file in the archive.

  3. Amos says:

    Thanks Markus, that’s awesome!

    What XML parser would you recommend using XStream on Android with?

    I currently use XStream DomDriver for compatability, but I understand that it is a bit slow. Have you tried this with the Kml parser that’s included with android? Other suggestions perhaps?

  4. Markus Junginger says:

    Hi Amos, thanks for the feedback. What exactly do you mean by “slow”? Reading? Writing? How large are your data sets? With some simple data objects, it was about 30ms in the emulator for writing a file including the IO. Which is OK for typical apps, I think. Haven’t measured reading the data back in though.

    I kicked out the other drivers except the DOM driver because I prioritized size over performance. XPP would have been faster, but would have introduced another dependency.

    I haven’t heard of a Kml parser, yet. Could you point me to some resources? Thanks!

  5. Amos says:

    My mistake: it’s “kxml”, not “kml”.
    There’s an xml parser called kxml2. It seems to be included in android.jar (org.kxml2.*) – open android.jar under “Referenced Libraries” in any Eclipse Android project, and it’s there.
    Info at .
    Seems to be a lightweight and fast parser, maybe that’s what android is using to do it’s xml parsing?

    I haven’t tried it yet myself, but maybe this parser works well and can be used instead of XStream’s DOM driver. This can perhaps reduce the size of the package even further.

    I’ve been working with XStream, their DomDriver and custom converters I created (which will probably not be required anymore, when I use your package). Performance seems fine so far, but maybe kxml can go even faster.
    I’ve started thinking about using xstream to serialize objects as strings and put them in Bundles when I need to save my Activity state when it is suspended (and work around the current android limitation which allows only strings and ints in bundles). Speed is more important in this case, and maybe kxml will have an advantage there.

    I haven’t tried these things out yet so I’m not comfortable talking about stuff I haven’t actually done (and I’m also new to the xml parsing and serialization world). I’ll play around with your package and see how it goes.
    If you see that kxml can help you, that could be great. And thanks for sharing your work!

  6. Markus Junginger says:

    I wonder if kXml will be supported officially, it’s just an undocumented feature so far. Anyway, since it’s a pull API, the XPP drivers (that I kicked out) might be a good starting point as they are a pull API, too.

    The activity/process life cycle can get complex and it depends on exactly you are up to. If you just want to save the data while your process is running, you could store them simply in some static variables. But if data has to be persisted even if your process gets killed, you could also consider files, SQLite and so on. Standard Java serialization may be an option here, too. Using XML here does not really have any advantages here, since you do not share data with other applications. If you want to use XStream for this: there’s also a binary format. Haven’t looked into this, but I guess it should be notably faster and produce much more compact files. I would be interested in this.

  7. dSelby says:

    this is great, i used xstream in a non android project, it´s so easy to use, now as it is obviously working, i´m trying to use it with android , how do i integrate it ? as a jar ? haven´t included any 3rd party libs yet…

  8. Ralf says:

    Hi Markus,

    we have a similar problem with xstream 1.3. It only happens sometimes: it seems to be a kind of magic!

    What change did you made for the patch in version 1.2.2?

    Thanks in advance for your help!

  9. redders says:

    how do i actually use this?
    previously when I used xstream i downloaded the .jar file and installed it via the prefrences>librarys page..

  10. Alexander says:

    Thank you for this effort. I use your XStream Android package 1.2.2 with success in my projects.
    I created a jar file from your Java source (compiled with Java-5 on the Mac). This jar can be included in the build path and exported via the Order and Export tab in the build path configuration. In the SDK simulator this works correctly.

    If somebody is interested in this jar, please let me know how to upload. Works fine!

    Thanks again and all the best,

  11. Android and XML De-/Serialization « Monsieur Rask says:
  12. Marco Schmitz says:

    Thanks a lot for this. I know it’s hard to reduce a framework to cope with android. I wrote a dependency tool myself in order to find out which classes do not fit. Can you explain how you reduced xstream?

    Marco Schmitz,

  13. Tu Dang says:

    It’s wonderful. Thank you very much for this.

  14. agherschon says:

    Awesome work here ! Thank you very much !

  15. [Android] GIS coordinates and Android says:

    […] you can find an “android version” of your library, you are very lucky (as I was for xstream). If not, you’ll have to create this android version yourself. Don’t forget to share it […]

  16. TienLQ says:

    Hi Alexander,
    Could you please show me how to get this jar. I really need it now.
    Thanks for your help.

    Best Regards

  17. vinh says:

    Great work,
    Thanks for your distribution

  18. Luis says:

    Great work. How can I obtain the patched lib? I have been trying to recompile the source with JDK 1.5 or greater but can’t get i to work on Dalvik.

  19. Joms says:


    I tried to package the source into a .jar file. but it seems to have a NULL POINTER EXCEPTION when i try to instantiate the XStream object. Can someone upload a sample project so i can have it tested? Thanks!

    Best Regards,

  20. Ryan says:

    Is there anyway to get this to work for JSON deserialization? My build works for regular Xstream Objects but fails when I try to initialize one with JettisonMappedXmlDriver(). Any help would be appreciated. Thanks

  21. alecrama says:

    If the xml file receives new fields that didnt exist in the object before how can I retrieved them from xml without be stopped by an exception ?