I’m trying to learn XStream, and I’ve followed the API as well as I can understand it, but the following code snippet
List<Rectangle> rectangleArray = new ArrayList<Rectangle>();
xstream = new XStream(new DomDriver());
List<Rectangle> rectangleArray2 = new ArrayList<Rectangle>();
rectangleArray.add(new Rectangle(18,45,2,6));
String xml = xstream.toXML(rectangleArray);
System.out.println(xml);
xstream.fromXML(xml, rectangleArray2);
System.out.println("new list size: " + rectangleArray2.size());
produces output
<list>
<java.awt.Rectangle>
<x>18</x>
<y>45</y>
<width>2</width>
<height>6</height>
</java.awt.Rectangle>
</list>
new list size: 0
and I cannot figure out why rectangleArray2 is not now a copy of rectangleArray. Any help?
Handling
ListthroughXStreamis bit trickier. To handle the list, you need to define a wrapper class to hold your list e.g.:Then add
aliasfor list toRectangleListclass asand register an implicit converter to manage the list as:
If you want your
<java.awt.Rectangle>to print as<rectangle>, register ans alias as below:Now use you
RectangleListclass for conversion, it should work fine.Final test code will look like:
This will print output as: