Skip to main content

The XStream library offers clean, easy XML serialization of POJOs.

Posted by scottschram on September 15, 2005 at 12:49 PM PDT

XStream serializes and restores very clean, readable XML from POJOs, like this:

public class Publisher {
	private String publisherID;
	private String name;
    // getters and setters...

    <name>Lorenz Corporation</name> 

Using two lines of code (although you can customize lots of things if needed.)

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

If you don't want it to include the fully qualified class name:


Add one line:

xstream.alias("Publisher", Publisher.class);

Collections look great, for example, this ArrayList of Listing objects:


Restoring the object is two lines (with optional aliases):

XStream xstream = new XStream();
// If you don't have fully qualified names, add one or more aliases:
xstream.alias("CatalogItem", CatalogItem.class);
catalogItems = (ArrayList) xstream.fromXML(theXMLString);

The XML suitable for editing by hand for configuration or if persisted to a file. It would be very easy to manipulate via XSLT or XQUERY. It makes a nice readable toString() for debugging.

License: BSD derived, commercial friendly.

Dependencies: none (!), XML Pull Parser optional to improve performance.


When using the Sun 1.4 (and 1.5) JVM, XStream can reinstantiate classes that do not have a default constructor. (Cool rule-breaking magic.) Other JVMs require a default constructor.

Check out XStream and the two minute tutorial.

XStream has worked very well for me. Thanks to Malcolm Davis for pointing out this terrific library.

Related Topics >>