The Source for Java Technology Collaboration
User: Password:



Scott Schram

Scott Schram's Blog

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

Posted by scottschram on September 15, 2005 at 12:49 PM | Comments (9)

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

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

<Publisher>
<publisherID>lorenz</publisherID>
<name>Lorenz Corporation</name>
</Publisher>

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:

<com.choralmusic.db.Publisher>

Add one line:

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

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

    <listings>
      <Listing>
        <pageRef>church_christmas</pageRef>
        <includeCover>false</includeCover>
      </Listing>
      <Listing>
        <pageRef>school_winter</pageRef>
        <includeCover>false</includeCover>
      </Listing>
    </listings>

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.

Limitations

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.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • See BindMark project for a list of 20+ libraries that do the same thing, some of them much faster than XStream.

    Posted by: kirillcool on September 16, 2005 at 03:53 AM

  • I also like XStreams very much. I'm using it to parse configuration files and to perform custom checks on the XML data, that are written in Java.

    Posted by: slohmeier on September 16, 2005 at 04:41 AM

  • XStream is a great framework, but it donĀ“t validate the XML (schema). It is very good for non-safe solution, a good alternative to the heavyweight JAXB. Next Java release (mustang) should come with JAXB embeeded, then all these frameworks will suffer a delay - just a guess.

    Posted by: felipegaucho on September 16, 2005 at 04:49 AM

  • I like stream ,specially easy of use. I like too much that it doesn't validate the XML schema, cuz I never modify the generated xml. I you read spanish check this: a xstream article

    Posted by: azuluaga on September 16, 2005 at 06:37 AM

  • In response to Kirill, the point for XStreams is that it's way simpler than the others. It won't fit every use case, but for simple POJO I/O, it's probably hard to get much cleaner than this. For instance, the high-performing JiBX starts talking about "binding definitions" almost immediately on their home page. That's needed sometimes, but it's a whole different level of complexity.

    And the bean XML serialization built into Java produces much larger and uglier XML than XStreams.

    Posted by: tjpalmer on September 16, 2005 at 07:46 AM

  • XStream is hands down way simpler than anything you'll find on BindMark. Anyone using classic serialization or XMLEncoder/XMLDecoder should make the move to XStream.

    Posted by: cowwoc on September 16, 2005 at 08:03 AM

  • Kirill, please note that XStream as opposed to JAXB, JibX, etx is not a binding tool it is serialization tool.

    Posted by: euxx on September 16, 2005 at 09:23 AM

  • Kirill, I searched "Web and Projects" for XStream before writing this article and I'm surprised that I didn't find anything (and it should find Bindmark). Is there some other place I should be searching? Also, I found that it's not necessary to write any classes to handle marshalling and unmarshalling, so, I wouldn't reduce the score for that on Bindmark's ease-of-use page.

    Posted by: scottschram on September 16, 2005 at 11:55 AM

  • Scott,
    It's not that you need to write a class for marshalling, but you need to provide your own set of classes to the marshaller (unlike schema-driven libraries). As stated on the home page and in FAQ, the ease-of-use is completely subjective (based on what i believe should be included), and as such the corresponding column is the last one with explicit header text.

    Posted by: kirillcool on September 16, 2005 at 01:30 PM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds