[jdom-interest] JDOM and XSL

Jan Peter Hecking jan at netgaroo.com
Thu Jan 25 05:45:05 PST 2001


On Thu, Jan 25, 2001 at 08:21:59AM +0200, Marco.Mistroni at nokia.com wrote:
> 	i have just started playing with JDOM and i have one question:
> has anyone ever used JDOM along with XSL?? or, how can i use it???

Basically I'm useing XSLT with JDOM as described in the JDOM
FAQ. But instead of Xalan-J 1 I'm using the beta of the new Xalan-J
2 where the API is changed quite a bit. But switching back to
Xalan-J 1 should be trivial. Below is the essential part of my code:

------------------------------ snip --------------------------------
// JDOM
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

// Xalan-J 2
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

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

public Document transform( Document sourceDoc, File xslt )
{
    XMLOutputter xmlOutputter = new XMLOutputter();
    SAXBuilder saxBuilder = new SAXBuilder();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Templates stylesheet = transformerFactory.newTemplates(
            new StreamSource( xslt ) );
    Transformer processor = stylesheet.newTransformer();

    PipedInputStream sourceIn = new PipedInputStream();
    PipedOutputStream sourceOut = new PipedOutputStream( sourceIn );
    StreamSource source = new StreamSource( sourceIn );

    PipedInputStream resultIn = new PipedInputStream();
    PipedOutputStream resultOut = new PipedOutputStream( resultIn );
    StreamResult result = new StreamResult( resultOut );

    xmlOutputter.output( sourceDoc, sourceOut );
    sourceOut.close();

    processor.transform( source, result );
    resultOut.close();

    Document targetDoc = saxBuilder.build( resultIn );
    return targetDoc;
}
------------------------------ snip --------------------------------

Hope this helps,
Jan

-- 
Jan Peter Hecking                  jhecking at netgaroo.com
University of Rostock     Department of Computer Science
Homepage: http://www.informatik.uni-rostock.de/~jhecking



More information about the jdom-interest mailing list