Five Minute Tutorial

You are viewing an old version (v. 2) of this page.
The latest version is v. 25, last edited on Feb 18, 2010 (view differences | )
<< View previous version | view page history | view next version >>

We demonstrate usage of this tool with a simple tutorial. Assume a Task class below with a prioritized field indicating whether it is a prioritized task:

package example;

public class Task {
  public boolean prioritized;
}

With XStream, we can serialize object of this class to XML like below:

Task task = new Task();
task.setPrioritized(true);
String xml = new XStream().toXML(task);
saveXmlToFileOrDatabase(xml);

Resulting XML will be:

<example.Task>
  <prioritized>true</prioritized>
</example.Task>

And you can deserialize the XML to get back task object:

String xml = readXmlFromFileOrDatabase();
Task task = (Task)(new XStream(new DomDriver()).fromXML(xml));

Everything is fine. Now we find a prioritized flag is not enough for a task, we need to enhance it to take a numeric priority field ranges from 1 to 10, so the task class becomes:

package example;

public class Task {
  public int priority;
}

However deserialization of previously saved xml is no longer possible since the new Task class is no longer compatible.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.