I would write briefly about a very good framework for the serialization and deserialization of Xml in Java applications: Xstream.
XStream is an API that allows the serialization of Java objects in XML, i.e. from a Java object, XStream performs a transformation to XML whose tags are the different attributes the object. Similarly, the deserialization allows the load of a Java object from an XML (file).
The main advantages of XStream are:
- The usability of the API,
- No necessity of change the code of objects to serialize,
- Speed of execution and low memory usage.
XStream is a JAR library available at the address http://xstream.codehaus.org, which must be put in the classpath for the examples to work. In the project in attachment, I have used the version xstream-1.4.2.jar.
XStream proposes an ease of use via methods to serialize and deserialize usable very easily. Moreover, the getters and setters in classes to serialize are not needed because XStream uses the introspection to getting the attributes declared private. XStream encodes itself the special characters in XML.
So, Xstream allows a lot of things like:
- modify some of the attributes to make them non-serializables,
- define an XML tag name different from that of the attribute,
- serialize the collections,
- the configuration of POJOs via Java annotations,
- the declaration of special converters,
Utilization of Serialization, Deserialization
The goal of this article is not to expose examples of serialization or deserialization because the site of Xstream (on codehaus.org) contains a lot of good examples:
The project test_xstream.zip in attachment contains 2 packages:
- com.ho.test.xstreamsite: most of the examples proposed on the site of Xstream,
- com.ho.test.realexamples: 2 personal complet examples.
In fact, I would write some words concerning the main use cases of serialization, deserialization which are below:
- generation of XML content simply, from a JAVA application,
- transmission of data in XML,
- making backup of data (for example: a configuration file),
- due to its flexible architecture, Xstream could be used to generate HTML of JSON flows, the handling of HTML, JSON mappings is as easy as handling of XML documents,
- data persistence???
for me, Xstream dosen’t allow the persistence of data but allows the swapping of data on a support ie Xstream can save and recover serialized and isolated objects, but only by using serialization/deserialization.
actually, the reason is very simple, the persistence of data means indirectly: integrity constraints, foreign keys management between objects, data mapping, data caching…etc. Yet, Xstream does not meet these needs/topics. The frameworks which propose most of these features are the ORM frameworks like Hibernate, iBatis..etc,