Xml Serializers For Java

At the moment I know only some xml serializers with different capabilities to write an object to an xml file and retrieve it back from it:

  1. JDK classes (XmlEncoder and XmlDecoder)
  2. XStream (BSD license)
  3. The Simple project (Apache License 2.0) it even handles cycles.
  4. Apache Digester (Apache license) can only read xml. To write xml use Betwixt.
  5. X2JB (LGPL)
  6. XmlFormat from javolution.org (BSD license)
  7. cedarsoft serialization (GPL with classpath exception)
  8. Xerces XmlSerializer (Apache license) is this used as the implementation for the first one? I discovered the class com.sun.org.apache.xml.internal.serialize.XMLSerializer in the jdk …
  9. Burlap from Caucho (Apache license) used as serialization format over http.
  10. JAXB (Apache license)
  11. Smooks (LGPL)
  12. Moose (LGPL)
  13. Datanucleus (Apache license) JDO persistence into relational db, oo-db, xml, excel, …
  14. vtd-xml (GPL)
  15. WAX (LGPL) xml writer
  16. … more libraries (and an old benchmark)
  17. … or web services tools

Although xstream is very easy to use (like XmlEncoder) it is not that convenient to use if you want that you xml fits to a special schema (if you have cyclic references). For the latter case you can use JiBX, JAXB, Castor or whatever …

Please write a comment of your choice and include some information about the library and why you chose it.


BTW: If you want to unit test reading and writing xml then xmlunit will help you!


10 thoughts on “Xml Serializers For Java

  1. If you use JDK6, it comes with an JAXB implmentation.

    MyData mydata = new MyData();
    File xml = new File(“mydata.xml”);
    javax.xml.bind.JAXB.marshal(mydata, xml);

  2. Thanks for this zman153!
    Can JAXB handle cyclic references out of the box? (e.g. for many to many relation ships)
    And you really have to regenerate some xml code (if I correctly understand) for any domain object changes?

  3. In xstream it was not easily possible for me to check the xml via xsd. E.g. if you have a many-to-many relation-ships. It will recursivly stream the objects, instead of just saying xref=”id” if an entity occurs as property of another entity.

    So the resulting xml is not only not checkable through xsd but it is also awkward to read 😦 and so it is not (easily) suited for configuration file serialization in my case.

    Before I created my own xml serializer (http://github.com/karussell/xvantage) I hacked xstream, so that many-to-many relationsships will be downgraded to many-to-one relationships (and on reading it will set the references back) …

  4. Pingback: Xvantage – Yet Another Xml Serializer! « Find Time for Java and more!

  5. StAX is part of the JDK and the API that most people should use. It offers XML pull. JAXB is a nightmare to maintain and add so much to your build time. Do you really need to map ALL the XML data to your Pojo? If you do then fair enough, but most of the time I think you will find you don’t.

  6. Pingback: Memory Efficient XML Processing not only with DOM « Find Time for the Karussell

Comments are closed.