Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

[jdom-interest] JDOM + XSLT solution

himank gupta

2005-10-04


hi guys

 

 i was able to do the transformation with the following code....

 

 

 

 public Document transform(Document sourceDoc, File stylesheetFile)
 {

     TransformerFactory transformerFactory = TransformerFactory.newInstance();
   Templates stylesheet = transformerFactory.newTemplates(new StreamSource(stylesheetFile));
     Transformer processor = stylesheet.newTransformer();
 

     //A holder for an XML Transformation source: a Document, Element, or list of nodes.
     JDOMSource source = new JDOMSource(sourceDoc);


    // A holder for an XSL Transformation result,
      JDOMResult result = new JDOMResult();

   

     processor.transform(source, result);

     Document newDoc = result.getDocument();

   

     // Display the results
     FileOutputStream resultOut = new FileOutputStream("c:\\out.html");
     XMLOutputter outp = new XMLOutputter();
     outp.output(newDoc, resultOut);

     return(newDoc);
 }

 

CHANGES from the previous apporach

 

-- i used Stream Result in place of JDOM Result

-- outputting the document after processing

i was able to have the new Document because JDOMResult let me do it.

StreamResult does not help me with this.

 

PREVIOUS CODE::

 

   // Set up the XSLT stylesheet for use with Xalan-J 2
   TransformerFactory transformerFactory = TransformerFactory.newInstance();
   Templates stylesheet = transformerFactory.newTemplates(new StreamSource(stylesheetFile));
   Transformer processor = stylesheet.newTransformer();
 
  //Take input Stream
   FileInputStream sourceIn = new FileInputStream(new File("c:\\javaxml\\contents.xml"));
   StreamSource source = new StreamSource(sourceIn);
  
  //Make Output Stream
   FileOutputStream resultOut = new FileOutputStream(new File("c:\\out.html"));
   StreamResult result = new StreamResult(resultOut);
   processor.transform(source, result);

 

Please suggest if someone was able to do it with StreamResult.

 


Yahoo! for Good
Click here to donate to the Hurricane Katrina relief effort.
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.