XML Serialization Made Simple

Developers frequently need serialization of objects to XML (and deserialization from XML to objects), and there are various solutions to this problem like Jibx and Jaxb. Simple is yet another solution which is very easy to use (hence the name Simple). Although it is simple, it is a powerful library which needs absolutely no configuration, and hence enables rapid development of applications. Simple also does not depend on several other libraries, and hence it is distributed as a single JAR file with a very small footprint (~245 KB).

Simple uses annotations for serialization and deserialization operations. These annotations describe how the objects should be serialized into an XML document , e.g., what are the elements and the attributes of these elements, what is the root of the XML document etc. Here is an annotated SimplePojo class:


@Root
public class SimplePojo {

	@Element
	private Date currentDate;

	@Element
	private String type;

	@Attribute
	private Integer id;

	@Attribute
	private Long version;

The @Root annotation specifies the root of the XML document, and the @Element and @Attribute annotations specify the elements, and attributes of the root element, respectively. After annotating our classes, we need to use instances of the org.simpleframework.xml.core.Persister class to serialize objects of these annotated classes. Given an object, the Persister instance is responsible for serializing the object to an OutputStream. To serialize a SimplePojo instance into file pojo.xml:


Serializer serializer = new Persister();
SimplePojo pojo = new SimplePojo(new Date(), "t", 1, new Long(5));

try {
	serializer.write(pojo, new File("pojo.xml"));
} catch (Exception e) {
	System.err.println("Serialization failed ...");
	e.printStackTrace();
}

To deserialize an XML document into an object, we again use the org.simpleframework.xml.core.Persister class:


Serializer serializer = new Persister();
try {
SimplePojo deserializedPojo = serializer.read(SimplePojo.class,
		new File("pojo.xml"));
} catch (Exception e) {
	System.err.println("Deserialization failed ...");
	e.printStackTrace();
}

It is also possible to serialize and deserialize object trees which are far more common in real/complex applications. Here is a ComplexPojo class which has a member of type SimplePojo:


@Root
public class ComplexPojo {

	@Attribute
	private String complexPojoAttribute;

	@Element
	private SimplePojo simplePojo = new SimplePojo();

ComplexPojo instances have members of class SimplePojo. Upon serialization of a ComplexPojo instance, Simple framework will serialize the whole object tree into the specified OutputStream. Finally, Simple framework is also able to serialize Pojos which contains list of other Pojos. Here is a more complex example:


@Root
public class MoreComplexPojo {

	@Element
	private ComplexPojo complexPojo;

	@ElementList(name = "PojoList")
	private List listOfSimplePojos = new ArrayList();

MoreComplexPojo instances will have members of type ComplexPojo, and they will contain a list of SimplePojo instances. In this example, the @ElementList annotation specifies that the corresponding field is a list of XML elements. This annotation can be used to specify one-to-many relations between Pojos. Upon serialization, the Simple framework will traverse the whole object tree and serialize all the elements into the specified OutputStream. You can download the source files for the example from here. For further features of the Simple framework, and other examples please see this Simple tutorial.

  • Share/Bookmark

About this entry