This is a very quick introduction to XStream. Skim read it to get an idea of how simple it is to convert objects to XML and back again. I am sure you’ll have questions afterwards.
Create Classes to be serialized:
Here are a couple of simple classes. XStream can convert instances of these to XML and back again.
public class Person{
private String firstName;
private String lastName;
private PhoneNumber phone;
private PhoneNumber fax;
public Person(String firstName, String lastName){
setFirstName(firstName);
setLastName(lastName);
}
public String getFirstName(){ return firstName; }
public void setFirstName(String firstName){ this.firstName = firstName; }
public String getLastName(){ return lastName; }
public void setLastName(String lastName){ this.lastName = lastName; }
public PhoneNumber getPhone(){ return phone; }
public void setPhone(PhoneNumber phone){ this.phone = phone; }
public PhoneNumber getFax(){ return fax; }
public void setFax(PhoneNumber fax){ this.fax = fax; }
}
public class PhoneNumber{
private int code; private String number;
public PhoneNumber(int code, String number){
this.code = code; this.number = number;
}
}
Note: Notice that the fields are private. XStream doesn’t care about the visibility of the fields. No getters or setters are needed. Also, XStream does not limit you to having a default constructor.
Initializing XStream:
To use XStream, simply instantiate the XStream class.
XStream xstream = new XStream();
You require xstream-[version].jar and xpp3-[version].jar in the classpath. XPP3 is a very fast XML pull-parser implementation. If you do not want to include this dependency, you can use the standard JAXP DOM parser instead.
XStream xstream = new XStream(new DomDriver());
Now, to make the XML outputted by the XStream more concise, you can create aliases for your custom class names to XML element names. This is only type of mapping required to use XStream and also this is optional.
xstream.alias(“person”, Person.class);
xstream.alias(“phone-number”, PhoneNumber.class);
Note: This step is optional. Without it XStream would work fine, but the XML element names would contain the fully qualified name of each class (including package) which would bulk up the XML a bit.
Serializing an object to XML:
Lets write a test class, in which we create a Person class and populate its fields:
Person sam = new Person(”Syed”, “Aslam”);
sam.setPhone(new PhoneNumber(95, “253265″));
sam.setFax(new PhoneNumber(95, “423140″));
Now, to convert it to XML, all you have to do is make a call to XStream:
XStream xstream = new XStream();String xml = xstream.toXML(sam);
And that’s it. The test class would look like this:
import com.thoughtworks.xstream.*;
public class XStreamTest{
public static void main(String[] args){
XStream xstream = new XStream();
Person sam = new Person(”Syed”, “Aslam”);
sam.setPhone(new PhoneNumber(95, “253265″));
sam.setFax(new PhoneNumber(95, “423140″));
String xml = xstream.toXML(sam);
System.out.println(xml);
}
}
The resulting XML would look like this:
<person>
<firstname>Syed</firstname>
<lastname>Aslam</lastname>
<phone>
<code>95</code>
<number>253265</number>
</phone>
<fax>
<code>95</code>
<number>423140</number>
</fax>
</person>
It’s that simple. Look how clean the XML is.
Deserializing an object back from XML
To reconstruct an object, purely from XML:
Person newPerson = (Person) xstream.fromXML(xml);