RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Get Moving with Java-XML Serialization Using XStream

As with most things, when it comes to serializing Java to XML and back, there is more than one way to skin the cat. In certain circumstances, Java-XML serialization can offer significant advantages over binding technologies such as JAXB and ultimately can save you development time. Learn the benefits and costs of Java-XML serialization using the excellent XStream library.

he XML format has become a de facto standard in contemporary IT work. Amongst all the XML-related technology advances, Java-XML binding and serialization has especially been of use to me because much of the work I do involves back-end business components. Java-XML binding and serialization technology has saved me significant development time and effort—and I know I'm not alone. If utilized judiciously, these technologies not only save time but they will make your system more flexible and responsive to change.

In this article I'll briefly discuss how Java-XML serialization works and show you how to take advantage of this technology to reduce your effort in processing XML. The resources section (see left column) lists various Java-XML binding technologies, but I'll be focusing on the open source XStream Java serialization library in this article and the accompanying code sample. You can download the free XStream library and the code for this article from the links in the resources section.

Distinguishing Binding from Serialization
Before digging in to how to use XStream to serialize Java objects, I'll first clarify the operational difference between Java-XML binding and Java-XML serialization. Both technologies are useful for converting XML back and forth to Java objects, however, Java-XML binding generally entails a pre-processing step that generates a Java source code file from an XML schema or DTD. Most binding libraries are sufficiently robust, such as Sun's JAXB and Apache's XMLBeans, that they also generate methods to marshal and unmarshal your XML back and forth to a Java object. So, if you control the build process and can generate your source then this solution is worth considering.

In contrast, Java-XML serialization employs a runtime conversion approach that relies upon the reflection API to convert a Java object to an XML document (and back again). Some libraries, like XStream, include robust APIs to programmatically manipulate the way the XML output is produced to make it more readable. Serialization is less effort to implement because there is no source code generation and compile steps. The downside of this simpler approach is that if the XML isn't generated with the same library there is no built-in means to ensure that the source XML will correctly deserialize to an instance of your target Java class.

What You Need
Java 1.4x or higher.
XStream open source XML serialization library available at http://xstream.codehaus.org/. You’ll need to include the XStream jar file xstream-1.1.2.jar in your classpath.
This article assumes that you're an advanced-beginner to intermediate Java programmer and somewhat conversant with XML technology.

Like many open source projects XStream is maintained by a distributed team of talented developers. My research indicates that the original author is Joe Walnes, who is one of the smart folks at London-based Thoughtworks. Thoughtworks is the company where Martin Fowler (author of "Refactoring" and "Analysis Patterns") is Chief Scientist. Whatever its exact origins may be, it's hard to imagine the library being any easier to use.

While XStream is a serialization library in that an object is converted to a non-Java representation, the classes that you're serializing don't need to specify the Serializable marker interface. XStream does its work using an object's internal fields, which are processed by a Converter-based design that I'll discuss shortly. In contrast, Sun's java.beans.XMLEncoder uses the JavaBeans public getter/setter API. When implementing a solution that relies upon reflection there can be a performance penalty but I found the library to be very fast in addition to being thread safe.

If you're new to the whole concept of Java–XML serialization you probably ought to know that changing class attributes or methods after you've serialized a class puts the object's deserialization seriously at risk. Therefore, it is another usability plus that XStream's developers made the effort to account for various 'class change' scenarios so you'll know what behavior you can expect depending on what changed and how. This topic is well documented on the XStream web site so I won't delve into it further here.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date