[jdom-interest] JDOM and XSL

Jan Peter Hecking jan at netgaroo.com
Fri Feb 9 08:43:12 PST 2001

On Fri, Feb 09, 2001 at 03:50:33PM +0100, Jan Peter Hecking wrote:
> I've attached example code for this set of transformations.

Sorry, forgot the attachment. Here it is.


Jan Peter Hecking                  jhecking at netgaroo.com
University of Rostock     Department of Computer Science
Homepage: http://www.informatik.uni-rostock.de/~jhecking
-------------- next part --------------
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.SAXOutputter;

import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import javax.xml.transform.Templates;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TemplatesHandler;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

public class XsltTransformer
    private SAXBuilder saxBuilder;
    private SAXOutputter saxOutputter;
    private SAXTransformerFactory saxTFactory;
    private TemplatesHandler templatesHandler;
    private XMLReader reader;

    public XsltTransformer()
        throws XsltTransformerException
            saxBuilder = new SAXBuilder();
            saxOutputter = new SAXOutputter( null );
            saxTFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
            templatesHandler = saxTFactory.newTemplatesHandler();
            reader = XMLReaderFactory.createXMLReader();
            reader.setContentHandler( templatesHandler );
        catch( Exception e ){

    public synchronized Document transform( Document sourceDoc,
                                            String stylesheet )
        throws XsltTransformerException
            // parse stylesheet to set up TemplatesHandler
            reader.parse( stylesheet );

            // create a new TransformerHandler that outputs a XML stream
            PipedInputStream resultIS = new PipedInputStream();
            PipedOutputStream resultOS = new PipedOutputStream( resultIS );
            Templates templates = templatesHandler.getTemplates();
            TransformerHandler handler= saxTFactory.newTransformerHandler(
                    templates );
            handler.setResult( new StreamResult( resultOS ) );

            // generate SAX events and send them to the TransformerHandler
            saxOutputter.setContentHandler( handler );
            saxOutputter.output( sourceDoc );

            // build a JDOM document from the resulting XML stream
            Document targetDoc = saxBuilder.build( resultIS );
            return targetDoc;
        catch( Exception e ){

More information about the jdom-interest mailing list