Add xstream support
Follow these steps to add an xstream library to a new project:
1. Select the new project in Eclipse project explorer and select Properties from the project menu, as shown in Figure 5 ).
Figure 5. Add an xstream Library
2. Click Add external jars and select the xstream-1.2.2.jar from the xstream_home/lib folder.
3. Click OK (as shown in Figure 6 ).
Figure 6. Added support for xstream
Figure 7 shows the project after xstream is added.
Serialized object
This simple example demonstrates how to use xstream to serialize/deserialize objects, including two classes: writer and reader. The writer class uses the xstream API to serialize an object of the employee type into XML and store it in a file (as shown in Listing 1 ).
Listing 1. Writer. Java
package com.samples;import java.io.FileNotFoundException;import java.io.FileOutputStream;import com.thoughtworks.xstream.*;public class Writer { public static void main(String[] args) { Employee e = new Employee(); //Set the properties using the setter methods //Note: This can also be done with a constructor. //Since we want to show that XStream can serialize //even without a constructor, this approach is used. e.setName("Jack"); e.setDesignation("Manager"); e.setDepartment("Finance"); //Serialize the object XStream xs = new XStream(); //Write to a file in the file system try { FileOutputStream fs = new FileOutputStream("c:/temp/employeedata.txt"); xs.toXML(e, fs); } catch (FileNotFoundException e1) { e1.printStackTrace(); } }}
Reader class reads the file, deserializes XML, and loads data into a Java object (as shown in Listing 2 ).
Reference from: http://java.chinaitlab.com/XMLBeans/777336_2.html