Although it is trivial to produce simple XML documents from Java code using println, this approach is not scalable; it becomes error prone and hard to maintain in any real project. After writing println based code for serveral projects we decided to craft a utility that would remove some of the pain from this process.
Before starting this project we reviewed some of the existing solutions for producing XML. DOM based solutions were out due to the streaming requirement. Using the SAX API directly was out due to the the 'easy to use' requirement. A couple of other utilities were found on the net that were designed to solve this problem but none of them were suitable.
Having crafted this utility to reduce our own pain, we decided to release it to open source so that other developers would not have to go through the same process.
This utility provides a simple interface that can be used to produce XML from Java code. It validates each addition to the XML document based on the current context before passing on the instructions to an output plug-in . The validation step ensures that the document produced is well-formed and that all namespace prefixes have been defined before they are used. If the client attempts to generate invalid XML an exception will be thrown.
There are currently two output plug-ins included in the project. The SAX plug-in produces SAX events via the content handler interface. The text plug-in produces a character stream of XML data. Some configuration parameters can be passed to the text plug-in to control line breaks, indenting etc.
The quality of the code is secured by a comprehensive suite of unit tests. The project is currently listed at BETA status on SourceForge because it has had only limited testing in real projects but a quick browse through the XmlOutput interface test suite should put your mind at ease as to the quality of the code.
This utility has been developed and tested using Java 1.4. There are no other dependencies in the production code. Note that the unit tests use JUnit 3.7 and the tests for the SaxSink plug-in use code from the MKMGroup, also licensed under Apache 2.0. An ANT 1.5 build script is included with the source.
Some improvements that could be made are:
This utility has progressed as far a our current needs require. Further development of new features will depend on our future needs and yours.