How to Serialize Objects to XML and Back Again with XStream?

BY MARKUS SPRUNCK

To create XML from Java objects is something that can be very error prone, e.g. doing the correct encoding of the various data types can be tricky. This is the reason why you never should do this by your own in Java. One of my favorite library to convert objects from and to XML is XStream. The following code sample describes some basic features.

How to Serialize Objects to XML and Back Again with XStream? 

The XStream implementation is simple, straightforward and has a small footprint. The following sample code is based on the current version 1.4.2 released in November 2011. 

It creates an instance of a class, serializes the object to XML and back again to a new object. To run the sample you need three libraries in the build path (i) xstream-1.4.2.jar, (ii) xmlpull-1.1.3.1.jar and (iii) xpp3_min-1.1.4c.jar. 

XStreamSample.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.sw_engineering_candies.sample;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
 
import com.thoughtworks.xstream.XStream;
 
public class XStreamSample {
 
    private String firstname;
    private String lastname;
    private Date created;
    private Double height;
    Collection<string> nickNames = new ArrayList<string>();
 
    public String toXML() {
        final XStream xs = new XStream();
        xs.alias("customer", XStreamSample.class);
        return xs.toXML(this);
    }
 
    @Override
    public String toString() {
        return "XStreamSample [firstname="
                + firstname + ", lastname="
                + lastname + ", created="
                + created + ", height="
                + height + ", nickNames="
                + nickNames + "]";
    }
 
    public String getFirstname() {
        return firstname;
    }
 
    public void setFirstname(final String firstname) {
        this.firstname = firstname;
    }
 
    public String getLastname() {
        return lastname;
    }
 
    public void setLastname(final String lastname) {
        this.lastname = lastname;
    }
 
    public Date getCreated() {
        return created;
    }
 
    public void setCreated(final Date created) {
        this.created = created;
    }
 
    public Double getHeight() {
        return height;
    }
 
    public void setHeightMetric(final Double height) {
        this.height = height;
    }
 
    public void addNickName(final String nickName) {
        nickNames.add(nickName);
    }
 
    public static void main(final String[] args) {
 
        // Step 1
        final XStreamSample firstInstance = new XStreamSample();
        firstInstance.setFirstname("Markus");
        firstInstance.setLastname("Sprunck");
        firstInstance.setCreated(new Date());
        firstInstance.setHeightMetric(1.93000);
        firstInstance.addNickName("Tiger");
        firstInstance.addNickName("Froschi");
        System.out.println("*********************************");
        System.out.println("Create instance of XStreamSample:");
        System.out.println(firstInstance.toString() + '\n');
 
        // Step 2
        System.out.println("*********************************");
        System.out.println("Create XML form instance:");
        final String xmlString = firstInstance.toXML();
        System.out.println(xmlString + '\n');
 
        // Step 3   
        System.out.println("*********************************");
        System.out.println("Create instance form XML:");
        final XStream xstream = new XStream();
        xstream.alias("customer", XStreamSample.class);
        final XStreamSample secondInstance =
                    (XStreamSample) xstream.fromXML(xmlString);
        System.out.println(secondInstance.toString() + '\n');
    }
} 

The output of the application should be something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
*********************************
Create instance of XStreamSample:
XStreamSample [firstname=Markus, lastname=Sprunck,
created=Tue Jul 10 20:23:28 CEST 2012, height=1.93,
nickNames=[Tiger, Froschi]]
 
*********************************
Create XML form instance:
<customer>
<firstname>Markus</firstname>
<lastname>Sprunck</lastname>
<created>2012-07-10 18:23:28.27 UTC</created>
<height>1.93</height>
<nicknames class="list">
<string>Tiger</string>
<string>Froschi</string>
</nicknames>
</customer>
 
*********************************
Create instance form XML:
XStreamSample [firstname=Markus, lastname=Sprunck,
created=Tue Jul 10 20:23:28 CEST 2012, height=1.93,
nickNames=[Tiger, Froschi]]

References

Change History

 Revision  Date  Author  Description
 1.0  Jul 10, 2012  Markus Sprunck   first version
 1.1  Aug 18, 2012  Markus Sprunck  improved layout for tablets
 1.2  Dec 16, 2014   Markus Sprunck  improved structure

Sponsored Link