I have 2 strings, an XML string I constructed using Java DOM interface, and an external XSL file I want to bind to that XML file. I tried using Java transform methods, but without luck (meaning I can't seem to find any solution for this on the web).
How do I take an XML file and an XSL file and make an HTML string out of them?
What I'm trying to do is to inject an XML page into my JSP page.
Just to clarify: This is done in a servlet, not in JavaScript.
A little more information:
I create the XML during runtime as a string, the XSL file I've got is stored on the server, what I want to do is to display the XML altered by the XSL file to the user when he clicks on a certain link on the site, and I want to embed that inside an existing JSP page (in order to maintain the standard look of the site).
This is what I've got so far:
String convertedXML = new String();
TransformerFactory factory1 =
TransformerFactory.newInstance();
Source xsl = new StreamSource("my.xsl");
Result result11 = null;
try {
Templates template = factory1.newTemplates(xsl);
Transformer transformer1 = template.newTransformer();
Source xml = new StreamSource(xmlString);
result11 = new StreamResult(convertedXML);
transformer1.transform(xml, result11);
} catch(Exception e) {
System.out.println("Not Good");
}
The last line before the catch throws the next error:
javax.xml.transform.TransformerException:
java.io.FileNotFoundException: at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(Unknown Source) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source) at controllers.UserController.schedulePage(UserController.java:394)
Could you put your file into WEB-INF and try to use following:
String path = "/WEB-INF/my.xsl";
ServletContext context = getServletContext();
InputStream xslIs = context.getResourceAsStream(filename);
Source xsl = new StreamSource(xslIs);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With