[jdom-interest] JDOM -> Xalan using SAX

Lasse Lindgård lasse at lintor.dk
Thu Jan 25 11:19:45 PST 2001


I just hacked this code together and I thought that I would submit it.
I wrote it this way because my old code (using Xalan 1.2 and the
DOMOutputter) seemed to be sluggish.
I didn't get a chance to profile this code just yet, but it might or might
not be faster than the original DOMOutputter code.

I hope that someone can suggest some improvements. I would have thought that
the code was faster than the old.

Enjoy
/Lasse

public final static void transformXalan2SAX(Document jdom, String
stylesheet, PrintWriter out)
	throws IOException, TransformerConfigurationException, SAXException,
JDOMException {
	// Instantiate a TransformerFactory.
	TransformerFactory tFactory = TransformerFactory.newInstance();
Honestly it seems a bit slower - but I don't know where the bottleneck is
yet.

	// Cast the TransformerFactory.
	SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
	// Create a ContentHandler to handle parsing of the stylesheet.
	TemplatesHandler templatesHandler = saxTFactory.newTemplatesHandler();

	// Create an XMLReader and set its ContentHandler.
	XMLReader reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
	reader.setContentHandler(templatesHandler);

	// Parse the stylesheet.
	reader.parse(stylesheet);

	//Get the Templates object from the ContentHandler.
	Templates templates = templatesHandler.getTemplates();
	// Create a ContentHandler to handle parsing of the XML source.
	TransformerHandler handler = saxTFactory.newTransformerHandler(templates);
	reader.setContentHandler(handler);

	Result result = new javax.xml.transform.stream.StreamResult(out);
	handler.setResult(result);

	SAXOutputter jdomOut = new SAXOutputter(handler);
	jdomOut.output(jdom);
}




More information about the jdom-interest mailing list