[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Apply XSL to XML (Java)

Posted on 2008-11-17
3
Medium Priority
?
1,093 Views
Last Modified: 2013-11-18
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)
0
Comment
Question by:angela_smith
3 Comments
 
LVL 4

Accepted Solution

by:
pellep earned 500 total points
ID: 22982910
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
 

Author Comment

by:angela_smith
ID: 22983272
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 22983428
Try
StreamSource xsltSource = new StreamSource(new FileReader(xslt)); // 'xslt' is the path to your xsl file

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question