You are viewing an old version (v. 4) 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 >>
<< 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, so we enhance the Task class to use a numeric priority field ranging from 1 to 10:
package example; public class Task { public int priority; }
However deserialization of previously saved xml is no longer possible since the new Task class is not compatible with previous version.