XML serialization use many factors, including object persistence and data transmission. However, some XML serialization technology to achieve them may be very complex. XStream is a lightweight, easy-to-use open-source Java ™ library sequence used to Java objects into XML or to convert back again. Learn how to set up XStream, how to use it to serialize and deserialize objects as well as from the XML configuration file to read configuration properties.

<! - START RESERVED FOR FUTURE USE INCLUDE FILES - ><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --><!-- END RESERVED FOR FUTURE USE INCLUDE FILES ->

The use of XStream will be able to meet most of the map without any Java object serialization. Of objects in the generated XML element name becomes the name of the class composition of the XML string in the element content. Use XStream serialization of the class do not need to achieve the Serializable interface. XStream is a serialization tool, rather than data binding tool, that can not XML or XML Schema Definition (XSD) file generation class.

Compared with other sequences tools, XStream has three outstanding features:

  1. XStream does not care about serialization / deserialization of the class field visibility.
  2. Serialize / deserialize a class field, no getter and setter methods.
  3. Serialize / deserialize a class do not need a default constructor.

Do not need to modify the class, the use of XStream can serialize directly to / inverse sequence of any third party class.

References):

  1. From the Eclipse website to download Eclipse. When installed unzip to the selected folder, using eclipse_home paper said. This article assumes that the use of Eclipse 3.3.
  2. From the XStream web site to download the latest stable version of XStream. When installed unzip to the selected folder, this paper xstream_home said. Assuming the use of XStream version is 1.2.2.
  3. From the Sun Web site to download Java Platform, Standard Edition (J2SE) Software Development Kit (SDK). Install to the selected folder, this paper java_home said. Assume that version is 1.5.0_05.

Figure 1 below).

Figure 2 below).

Figure 3 below).

Figure 4 shows the new Java project.

Figure 5 below).

Figure 6 below).

Figure 7 shows, after adding XStream to support the project.

Top

Listing 1 below).

Listing 2 below).

Listing 3 shows the Employee object structure.

Top

References) provides for the preparation of conversion process-oriented curriculum.
Most applications have to use some properties. These properties may be to connect the data source name or the location of the log file. Profile best suited to store such data, and thus do not need to modify these properties to recompile the application when the application makes it easier to maintain. The following example shows how to use XStream in the application configuration file to read from the XML configuration properties.

XML data binding usually generate Java objects from XML files. Generating an object using XStream omit this step. Only need to write a Java class to class field is mapped to want to read XML elements. This example uses the configuration file as shown in Listing 4.

Listing 5 shows ConfigReader class to read the configuration file and loaded into the Java object properties.

Top

Reference) can learn more about aliases, comments, and conversion process. Alias and conversion programs can allow you complete control over the generated XML.

Set

Description Name Size Download Method
Java source code for this article x-xstreamcode.zip 3KB HTTP


http://www.ibm.com/developerworks/cn/xml/x-xstream/