[jdom-interest] JDOMSource and JDOMResult can produce nothing!

Ken Rune Helland kenh at csc.no
Mon Jun 25 01:55:27 PDT 2001


At 02:59 PM 6/22/2001 +0100, Bruno Antunes wrote:
>Hi. In fact I have missed the fact that I was transforming a JDOM 
>Document, on another
>JDOM Document, so this new transformed JDOM Document must be a valid XML 
>document. (why
>JDOM does not generate and Exception when a non valid XML transformation 
>is performed?)
>
>So my point now, is what is the most elegant way to transform a a JDOM 
>Document in a
>non XML file, using XSLT.


Use JDOMSource, but StreamResult:

public void transform(Document doc, String xslName, Writer writer)
         throws IOException, JDOMException {
     try {
         Transformer transformer = TransformerFactory.newInstance()
                 .newTransformer(new StreamSource(getXSLStream(xslName)));

         transformer.transform(new JDOMSource(doc), new
               StreamResult(writer));

     } catch (TransformerException e) {
        throw new JDOMException("XSLT Transformation failed", e);
     }
}

This is also faster, in my simple tests using JDOMSource is 30-60% faster
than using StreamSource not counting the (int his case unnessesary)
serializing time


KenR




More information about the jdom-interest mailing list