Like Java Serializing, but in XML
With the discovery of XStream, I'm not sure you would need this library anymore. However, it's lightweight and still pretty powerful, so knock yourself out if it's helpful. Nearly all the "external" classes are built in to Java now except for the megginson...DataWriter class that makes "pretty" XML. That's optional anyway.
This collection of Public Domain* software helps you convert your Java objects to/from XML by providing an Xmlizable framework similar to Java's Serializable framework.
Through a series of SAX2 Content Handlers and some object parsers, Maps, Collections, Arrays, and primitives can be converted to XML. By implementing the Xmlizable interface in your own classes, you can ensure that your objects have their own representation in XML. Alternatively, an object that is only Serializable, will be serialized and converted to Base64 before stored in XML form. (The Base64 converter is a bonus. If you were looking for a good Public Domain Base64 converter, you're welcome to use this one!)
To use one of the help methods for one-liner reading of XML files from disk, you'll need either the Java API's for XML Processing or JDK1.4. If you have another XML parser that you like, you can just as easily use these SAX2 Content Handlers with that.
The obligatory Javadoc-generated API for Xmlizable. The ParserUtilities class is where you should start.
Saving an object to disk in XML form is as easy as
ParserUtilities.saveObject( myObject, myFile );
myObject = ParserUtilities.readObject( myFile );
Your Xmlized object might look something like this:
<?xml version="1.0" encoding="iso-8859-1"?> <h:object h:class="java.util.HashMap" xmlns:h="http://www.iharder.net/xmlizable"> <h:entry> <h:key><h:object h:class="java.lang.String">AAPL</h:object></h:key> <h:value><h:object h:class="java.lang.Double">20.38</h:object</h:value> </h:entry> ... <h:entry> <h:key><h:object h:class="java.lang.String">SUNW</h:object></h:key> <h:value><h:object h:class="java.lang.Double">24.12</h:object</h:value> </h:entry> </h:object>
XSL Helper Stylesheets
Here are some XSL transformations to make nice looking HTML from your Xmlized files. Even if you use nothing else from this page, perhaps these examples of XSL files will be helpful.
- default.xsl . A skeleton HTML document that imports the following XSL documents.
- identity.xsl . As the most general XSL template, it copies elements verbatim to the output if no better XSL template is encountered (such as those below).
- object.xsl . Wraps a generic <h:object> tag in a <span> and gives the <span> a title attribute for browsers that support tooltips. The title includes the object's name, if given (see map.xsl below), and the object's class name.
- array.xsl . Matches <h:object-array> and <h:object-array> tags. The template creates an unordered-list <ul> with each object in the array being wrapped in its own <li> tag.
- map.xsl . Matches <h:object> tags whose class is one of Java's Map implementations such as java.util.HashMap and java.util.Properties. A table is constructed with table headers "key" and "value", though these values can be over-ridden by applying the template with the <xsl:with-param> tag. Each key/value pair in the Map is a row in the table. When <xsl:apply-templates> is called on the value object, the key is passed as the object's name which may subsequently appear in tooltips (see object.xsl above).
- collection.xsl . Matches <h:object> tags whose class is one of Java's Collection implementations such as java.util.ArrayList and java.util.Vector. The template creates an unordered-list <ul> with each object in the collection being wrapped in its own <li> tag. The objects' templates are applied with the Collection's name, if there is one, and the order in which the object appears in the Collection. For instance, the first object's name might be things.to.buy.
A Note About Public Domain
I have released this software into the Public Domain. That means you can do whatever you want with it. Really. You don't have to match it up with any other open source license &em; just use it. You can rename the files, move the Java packages, whatever you want. If your lawyers say you have to have a license, contact me, and I'll make a special release to you under whatever reasonable license you desire: MIT, BSD, GPL, whatever.
*Actually there are three Java files that are under a BSD-style license from www.dom4j.org. These are a SAX2 version of Aelfred, an XML parser. If you don't want to hassle with non-Public Domain software feel free to review the one method (readObject) in ParserUtilities.java that uses Aelfred. There is commented-out code that uses Sun's Java API's for XML Processing (JAXP) available if you use JDK 1.4 or greater. The BSD License is very easy to work with, so you shouldn't have any problem using the xmlizable.jar file in your own work, but again, feel free to use JAXP if you like.
blog comments powered by Disqus