Xstream is a simple Java-based serialization/deserialization library to convert Java Objects into their XML representation. It can also be used to convert an XML string to an equivalent Java Object. It is a fast, and efficient extension to the Java standard library. It’s also highly customizable. For this tutorial, we assume Java and environment variables are properly installed in your local environment.
Download the XStream Archive
Download the latest Xstream archive from this link. If you downloaded the jar then you’ve to set the CLASSPATH variable manually or with the help of IDE, (or)
With Maven:
If you added it to your project through the maven central repository, then you don’t need to set the CLASSPATH variable, maven will do this automatically for you.
<dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.19</version> </dependency>
Set CLASSPATH Variable
Manually:
Linux:
export CLASSPATH=$CLASSPATH:$XStream_HOME/xstream-1.4.19.jar:
Windows:
Set the environment variable CLASSPATH to
%CLASSPATH%;%XStream_HOME%\xstream-1.4.19.jar;
Mac:
export CLASSPATH=$CLASSPATH:$XStream_HOME/xstream-1.4.19.jar:
(OR)
With the help of an IDE: In IntelliJ IDEA follow the below steps:
Steps:
Right Click on Project -> Open Module Settings -> Libraries -> Click on ‘+’ -> Add Xstream Jar -> Apply and OK
XStream Application
Make sure you’ve already set up Spring core and web jar files
Java
| importlombok.Getter; @GetterpublicclassEmployee {    privateString firstName;    privateString lastName;    privateintsalary;    privateintage;    privateString gender;     publicEmployee(String firstName, String lastName,                    intsalary, intage, String gender)    {        this.firstName = firstName;        this.lastName = lastName;        this.salary = salary;        this.age = age;        this.gender = gender;    }} | 
Java
| importcom.thoughtworks.xstream.XStream;importcom.thoughtworks.xstream.io.xml.DomDriver;importjava.io.FileWriter;importjava.io.IOException;importorg.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplicationpublicclassXStreamExampleApplication {    publicstaticvoidmain(String[] args)        throwsIOException    {        // Initializing XStream with Dom driver        XStream xStream = newXStream(newDomDriver());         // Now, to make the XML outputted by XStream more        // concise, you can create aliases for your custom        // class names to XML element names. This is the        // only type of mapping required to use XStream and        // even this is optional.        xStream.alias("employee", Employee.class);         Employee e1 = newEmployee("Sanyog", "Gautam", 1000,                                   19, "Male");         // Serializing a Java object into XML        String xml            = xStream.toXML(e1); // Converting it to XML        System.out.println(xml);         // Java Object to a file        try(FileWriter writer = newFileWriter(                 "/home/anurag/xstreamExample.xml")) {            xStream.toXML(e1, writer);        }        catch(IOException e) {            e.printStackTrace();        }    }} | 
As you can see, we get our clean XML
<employee> <firstName>Sanyog</firstName> <lastName>Gautam</lastName> <salary>100</salary> <age>19</age> <gender>Male</gender> </employee>
We can deserialize our object from that XML
Java
| importcom.thoughtworks.xstream.XStream;importcom.thoughtworks.xstream.io.xml.DomDriver;importjava.io.IOException;importorg.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplicationpublicclassXStreamExampleApplication {    publicstaticvoidmain(String[] args)        throwsIOException    {        // Initializing XStream with Dom driver        XStream xStream = newXStream(newDomDriver());         // We need to configure the security framework in        // XStream, so it deserializes the object from the        // XML        xStream.allowTypes(newClass[] { Employee.class});         // Now, to make the XML outputted by XStream more        // concise, you can create aliases for your custom        // class names to XML element names. This is the        // only type of mapping required to use XStream and        // even this is optional.        xStream.alias("employee", Employee.class);         Employee e1 = newEmployee("Sanyog", "Gautam", 1000,                                   19, "Male");         // Serializing a Java object into XML        String xml            = xStream.toXML(e1); // Converting it to XML         // Deserializing a Java object from XML        Employee employee = (Employee)xStream.fromXML(xml);         System.out.println("First name of Employee:  "                           + employee.getFirstName());        System.out.println("Last name of Employee:   "                           + employee.getLastName());        System.out.println("Employee's age:          "                           + employee.getAge());        System.out.println("Employee's gender:       "                           + employee.getGender());        System.out.println("Employee's salary:       $"                           + employee.getSalary());    }} | 
We get our object back
 
Deserialized object from XML


 
                                    







