Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Apply XSL to XML (Java)

 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use core java APIs (i.e. JAXP) to achieve this.

What you need to do ultimately is call javax.xml.transform.Tranformer.transform(
javax.xml.transform.Source, javax.xml.transform.Result)

Some other hints to solve the problem the way you want is the
StringReader class and the StringWriter class.

Another hint to a different approach is that the implicit "out" object in a JSP is a Writer (more specifically a PrintWriter).

Google for some sample programs using some relevant keywords which
will make your life a bit easier.

Also here is a link to the JSE 6 API. Check the javax.xml.tranform and javax.xml.tranform.stream packages.
 
Anjali S Sharma
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
I did this

but the problem seems to be this line
StreamSource xsltSource = new StreamSource(new StringReader(xsl));
I have path to xsl in the variable "xsl". But I guess the paramter expects the XSL content. How do I get it here?
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the xsl try some type of InputStream..
 
reply
    Bookmark Topic Watch Topic
  • New Topic