Apply XSL to XML (Java)

I have my XML in a StringBuffer variable.
Example,
StringBuffer sb = new StringBuffer();
sb = sb.append("<node1><node2><node2><node3></node3></node1>") ;

This application is deployed on Tomcat. Inside the web application, I have my XSl.
Can someone please let me know how can I apply this XSL to the above xml, so that I get the HTML result in a variable (which I'll send to the JSP to be displayed)
angela_smithAsked:
Who is Participating?
 
pellepConnect With a Mentor Commented:
Try this and play around with it.
StringBuffer sb = new StringBuffer();
sb = sb.append("<node1><node2><node2><node3></node3></node1>") ;
String xslt = ""; // ADD YOUR XSLT;
StreamSource xmlSource = new StreamSource(new StringReader(sb.toString()));
StreamSource xsltSource = new StreamSource(new StringReader(xslt));
 
StringWriter swResult = new StringWriter();
StreamResult result = new StreamResult(swResult);
 
 
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 
Document domDoc = builder.parse(new InputSource(new StringReader(sb.toString()));
 
Transformer t = TransformerFactory.newInstance().newTransformer(xsltSource);
 
t.transform(xmlSource, result);
String html = swResult.toString();

Open in new window

0
 
angela_smithAuthor Commented:
Thank you

>>String xslt = ""; // ADD YOUR XSLT;

I have path to my XSLT which is on the Server. How do I get it's contents into this variable?
0
 
CEHJConnect With a Mentor Commented:
Try
StreamSource xsltSource = new StreamSource(new FileReader(xslt)); // 'xslt' is the path to your xsl file

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.