Details
-
Type: Wish
-
Status: Closed
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
Description
Now, we can serialize several Objects in the same stream, yet we can't deserialize them. so before to close the stream, it would be interessant if we can call "fromXML" several times to read all objects from the stream. And perhaps, this method could return null when the stream has no more elements, or like collections, could have a method "hasNext".
Regards,
Gabriel Barbier.
Hi Gabriel,
Here's a way you can do this....
You have to wrap all your objects in a stream in a single root node - this is a limitation of XML and the underlying parsers.
To serialize:
HierarchicalStreamWriter writer = new PrettyPrintWriter(out);
{ xstream.marshal(someObject, writer); // each object }writer.startNode("stuff"); // enclosing root node
for(.... whatever)
writer.endNode(); // close root node
out.close();
This will produce something like:
<stuff>
<myThing>...</myThing>
<myThing>...</myThing>
<myThing>...</myThing>
</stuff>
To deserialize:
HierarchicalStreamReader reader = new XppReader(in); // starts in <stuff>
{ reader.moveDown(); // move down into <myThing> Object someObject = xstream.unmarshal(reader); reader.moveUp(); // move up again }while(reader.hasMoreChildren())