Hi,
I wrote a class that uses Apache xalan to process a given XML file by applying a XSLT stylesheet (code below). The main method works fine when it writes the results to a file, but in the second method, nothing happens when I try to send the results to a String via StringWriter.
Everything I found online about xalan only showed writing the results to a file. Writing to a file seems very wasteful when the calling program will need to immediately re-read it. I'd think I should put the results into a String and pass that back to the calling program, without ever saving it to disk.
Is file i/o so cheap enough I should stick with that method? If not, how can I get the value into a String to pass?
Thanks for your help,
Elias
Code:package com.newburycomics; import java.io.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import org.apache.xalan.xsltc.trax.XSLTCSource; import org.w3c.dom.Document; public class XSLTApplicator { //this is the main method that works public static void main(String args[]) { javax.xml.transform.TransformerFactory myFactory = javax.xml.transform.TransformerFactory.newInstance(); //this is the XSL stylesheet String strFileName = args[0]; File fileXSL = new File(strFileName); StreamSource srcXSL = new StreamSource(fileXSL); try { Transformer myTransformer = myFactory.newTransformer(srcXSL); //this is the source XML file strFileName = args[1]; File fileXML = new File(strFileName); StreamSource srcXML = new StreamSource(fileXML); //the destination XML file strFileName = args[2]; File fileResult = new File(strFileName); StreamResult rsltResult = new StreamResult(fileResult); try { myTransformer.transform(srcXML, rsltResult); } catch (TransformerException xTE) { System.out.println(xTE); } } catch (TransformerConfigurationException xTCE) { System.out.println(xTCE); } }//main //this method compiles but //doesn't return the XML //I call this method from a wrapper class //just so I can have the two in the same file //doesn't change if I make it the main method public static String ApplyStylesheet(String strXSL, String strURL) { String strResult = "This will hold the resulting XML"; javax.xml.transform.TransformerFactory myFactory = javax.xml.transform.TransformerFactory.newInstance(); //get XSL location from args String strFileName = strXSL; File fileXSL = new File(strFileName); StreamSource srcXSL = new StreamSource(fileXSL); try { Transformer myTransformer = myFactory.newTransformer(srcXSL); //strURL represents a local file right now but will be a web services request later File fileXML = new File(strURL); StreamSource srcXML = new StreamSource(fileXML); //File fileResult = new File(strFileName); StringWriter swResult = new StringWriter(); StreamResult srResult = new StreamResult(swResult); try { //srResult.setWriter(swResult); //am I supposed to do this here? didn't work, //even when I removed swResult from the StreamResult constructor myTransformer.transform(srcXML, srResult); //strResult = "worked this far"; swResult.write(strResult); // this line doesn't change the value of strResult //I also tried removing this line when the setWriter method was active } catch (TransformerException xTE) { System.out.println("error: " + xTE); } } catch (TransformerConfigurationException xTCE) { System.out.println("error: " + xTCE); } return strResult; }//ApplyStylesheet }//class


Reply With Quote
Bookmarks