iHarder.net

Xmlizable

Support This Project

Like Java Serializing, but in XML

Download now!

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.

Examples

Saving an object to disk in XML form is as easy as

    ParserUtilities.saveObject( myObject, myFile );
And reading the file back is just as easy:
    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>
 

Sample Output

Here is a sample xml file that came from Xmlizable. There is an XSL directive in it, so you might see the fancifully-formatted version.

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.

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