posted 12 years ago
In the past I have used the rather obscure technique of taking an identity transformation and feeding it a stream of SAX events. When you use a SAX parser it passes a stream of SAX events to your ContentHandler, which means in practice that it calls your ContentHandler's startDocument and startElement and characters methods (etc) in the right order. My technique is to turn that around. Here's a brief outline extracted from some of my code:
This is just a crude outline, notice that I've hard-coded constants which would normally be passed to the code as method parameters, but it is working code which outputs a simple XML document.
An advantage of this is that it leverages the knowledge of the built-in Java classes so that you don't have to concern yourself with escaping of ampersands and quoting of attributes and all of that tedious stuff.