[ANN] Small, simple Java-based XML output library
Well, I'm unemployed and not getting any job offers, so I'm using my free time to pull together some disparate snippets of code -- and working on some new stuff -- packaging it up and making it available as open source. As part of that, I have a very small, simple library for outputting XML to SAX event handlers. The goal was to come up with a simple, compact, and readable notation for creating SAX-based XML output with a reasonable amount of built-in error checking. I tried to come up with an API that comes as close as possible to the simplicity, compactness, and readability of the markup being produced. There is obvious limits to how far you can with this with a Java API (without resorting to a template language), but I thought I'd see what I could come up with. Here is what I came up with: Consider the following XML snippet: <department> <employee id="500"> <firstName>Kilgore</firstName> <lastName>Trout</lastName> </employee> </department> The code to produce this directly using SAX would be quite ugly and verbose, in spite of the simplicity of the markup being produced. Code to produce this using this API, though, could look something like this: ResultFactory factory = new ResultFactory(); DocumentResult result = factory.newDocumentResult(new StreamResult(System.out)); ElementResult dept = result.element("department"); ElementResult emp = dept.element("employee") emp.attribute("id", "500"); emp.element("firstName").text("Kilgore"); // child element with text content emp.element("lastName").text("Trout"); // child element with text content // output the accumulated result tree result.end(); Alternatively, we could emit each element as it is completed, and could use an indentation style that emulates (somewhat) the XML structure we wish to generate: ResultFactory factory = new ResultFactory(); DocumentResult result = factory.newDocumentResult(new StreamResult(System.out)); result.element("department") .element("employee").attribute("id", "500").end() .element("firstName") .text("Kilgore") .end() .element("lastName") .text("Trout") .end(); // bail out here and output the remaining result tree result.end(); The idea is we have these opaque result objects with factory methods corresponding to specific XML constructs. You can use these methods to effectively iterate along an axis appending XML constructs. The "element" method returns an ElementResult that has methods that let you start adding constructs along either the attribute axis or child axis. Other result types offer methods to append constructs along one particular axis. Invoking "end()" flushes accumulated output out to the SAX handlers, then steps up the parent axis to the parent result. This is part of a larger toolkit I'm working on. However, it has no dependencies on the rest of the toolkit, and has a different focus, so I thought I'd go ahead and release this and see if anyone finds it of any interest. This may be downloaded from my SourceForge project web site: http://sourceforget.net/projects/swan Comments and feedback are, of course, welcome.
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format