|
|
||
Scott Schram's BlogWeb Services and XML ArchivesThe XStream library offers clean, easy XML serialization of POJOs.Posted by scottschram on September 15, 2005 at 12:49 PM | Permalink | 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:
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
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. | ||
|
|