[jdom-interest] Passing JDOM to JAXP using SAX

Laurent Bihanic laurent.bihanic at atosorigin.com
Fri Apr 30 04:52:51 PDT 2004


Why not using JDOMSource (to feed the input document to the XSLT processor) 
and JDOMResult (to gather XSLT output as a JDOM document/node list) ? For more 
information, see package org.jdom.transform.

    public static List transform(Document doc, String stylesheet)
                                         throws JDOMException {
      try {
        Transformer transformer = TransformerFactory.newInstance()
                              .newTransformer(new StreamSource(stylesheet));
        JDOMSource in = new JDOMSource(doc);
        JDOMResult out = new JDOMResult();
        transformer.transform(in, out);
        return out.getResult();
      }
      catch (TransformerException e) {
        throw new JDOMException("XSLT Trandformation failed", e);
      }
    }

You can also use a Templates object and pass it as an argument :

    public static List transform(Document doc, Templates stylesheet)
                                         throws JDOMException {
      try {
        JDOMResult out = new JDOMResult();
        stylesheet.newTransformer().transform(new JDOMSource(doc), out);
        return out.getResult();
      }
      catch (TransformerException e) {
        throw new JDOMException("XSLT Trandformation failed", e);
      }
    }

Laurent


Daniel Purucker wrote:
> Hi,
> i want to transform an jdom-document with xslt. Because i'm not able to 
> pass JDOM-Output directly to JAXP, i'm using the workaround proposed by 
> Eric Burke, to transform JDOM into SAX.
> That's the code, but i allway get wired error-messages...
> 
> 
> public static SAXResult JDOMtoSAX(TransformerFactory transFact, String 
> stylesheet, Result ergebnis) {
> 
>     if(transFact.getFeature(SAXTransformerFactory.FEATURE)) {
>       SAXTransformerFactory stf = (SAXTransformerFactory) transFact;
> 
>       // "xsltTransform.java": cannot resolve symbol: method 
> newTransformerHandler     // (java.lang.String)in class 
> javax.xml.transform.sax.SAXTransformerFactory       TransformerHandler 
> transHand = stf.newTransformerHandler(stylesheet);
>       transHand.setResult(ergebnis);
>       // "xsltTransform.java": cannot resolve symbol: class SAXOutputter 
> in class     //xtree.xsltTransform at line 97, column 7
>       SAXOutputter saxout = new SAXOutputter(transHand);
>     }
>     return saxout.output(jdomdoc);
> }
> 
> What's wrong?
> the "stylesheet" has to be an instance of 
> "javax.xml.transform.Templates", but how do i create this?
> Any help would be greatly appreciated. Thanx in advance,
> 
> Daniel



More information about the jdom-interest mailing list