Friday, January 23, 2009

[Tech] An easy to use XML Serializer

XML processing is an important part in present software systems, especially when communicate with other software components in the IT infrastructure. Pretty often you must provide your object data as XML. The Open Source market provides a wide range of XML tools, above all XML mapping tools, like Castor, JAXB and others. A very intersting and compact tool, existing since 2004 is XStream hosted on Codehaus. XStream is a proved XML serializing library and provides the following key features:
  • Easy to use API (see example)
  • You do not need explicit mapping files, like other serializing tools
  • Good performance
  • Full object graph support
  • You can modify the XML output
Let us consider a simple business object, person, implemented as a POJO (taken from the XStream homepage):

public class Person {
private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
// ... constructors and methods
}

public class PhoneNumber {
private int code;
private String number;
// ... constructors and methods
}
In order to get a XML representation of the Person object we simple use the XStream API. We also set alias names which are used in the output XML.
XStream xstream = new XStream();
xstream.alias("person", Person.class);
xstream.alias("phonenumber", PhoneNumber.class);
String resultXml = xstream.toXml(myPerson)
When creating a new instance of the person object an serialize it via xstream (toXml) we get the following XML result. As we can see our alias names are used.

<person>
<firstname>Joe</firstname>
<lastname>Walnes</lastname>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
</person>

The example illustrates that the framework is very compact and easy to use. Look at the 2 Minute tutorial on the XStream homepage to get more examples. You can also implement custom converter and transformation strategies to adapt XStream to your requirements.

Have fun with XStream.

3 comments:

Anonymous said...

I agree with you that XStream is very simple to use. The disadvantage that I experienced is that it is not possible to validate the XML files. It does not check for you that required attributes are used.
Simple (http://simple.sourceforge.net/) is another simple (and even more compact) XML serializer. It uses annotations to identify the elements that should be serialized. Arguments to the annotations allow to define attributes as optional or required.

But I had one problem with Simple: Changing the resulting XML is very hard and not always possible.
What do you know about modifying the XML output of XStream?

Anonymous said...

XStream is nice and simple, I think that's its main strength really.

I think that validation is more important when reading XML streams than when writing them. Of course validation is important but adding it to XStream might start to take away from its simplicity.

To get the best of both worlds perhaps it would be possible to use XStream to write non validated XML and include a unit test within your testing regime that uses a validating XML reader to verify that the output XML validates (under different conditions)?

Markus Demolsky said...

Hi everybody, sorry for the late response

@Andreas: The XML output can be easily modified by using the alias API. I agree with you that in some cases this can be a little bit tricky, but most of the cases can be resolved. In special cases, you can register your own converter for datatypes, which is not so hard. I never heard about http://simple.sourceforge.net/, thank your for the link.

I primary use XStream to produce XML output. As Kevin mentioned you can validate the XML with a validation framework and route the valid XML to XStream in order to serialize it.

To summarize, I think XStream is an ideal candiate when you want to produce XML.