From what I understand from http://x-stream.imtqy.com/tutorial.html (I had never worked with XStream before), you first need to define your types. Casting for String is definitely wrong, you probably need an individual type (depending on what's inside your random XML), then you need to map the XML tags to your members:
eg.
xstream.alias("person", Person.class); xstream.alias("phonenumber", PhoneNumber.class);
means that it matches the "person" tag inside your XML with your Person class.
For derserialize you can:
RandomTree myRandomTree = (RandomTree)xstream.fromXML( xml );
In addition, you close your thread twice, and you probably want to do this in the finally block :)
Edit: by reading your comment above ...
Your task consists of two steps:
- Deserialization
- Serialization
To serialize your object, you must first deserialize it from your input file.
To output your object as a string, simply do
String xml = xstream.toXML( myRandomTree );
source share