Home > Java > Playing with XStream

Playing with XStream

September 10th, 2006

I am building a small client/server app and was trying to figure out how to transfer object state between the client and the server. At first I thought about using serialized objects or maybe Java RMI, but then I found a little library that transfers object state via XML. The library is called “XStream”:http://xstream.codehaus.org/.

With just a few bits of code, I can transfer an entire object from.

XStream xstream = new XStream();
String xml = xstream.toXML(anObject);

Then I just need to transfer the resulting String. Then, once transfered, to convert it back into an object, I just need to call the following:

Foo foo = (Foo)xstream.fromXML(xml);

I am sure that there are more tricks you can do with it, but for transfering object state, XStream makes things incredibly simple. On “theServerSide.com”:http://www.theserverside.com there have been a few threads about converting Objects to XML and XStream always comes up as a recommendation. I definitely second it.

Categories: Java Tags:
Comments are closed.