Apply XSL to XML (Java)

Posted on 2008-11-17
Last Modified: 2013-11-18
I have my XML in a StringBuffer variable.
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)
Question by:angela_smith
    LVL 4

    Accepted Solution

    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


    Author Comment

    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?
    LVL 86

    Assisted Solution

    StreamSource xsltSource = new StreamSource(new FileReader(xslt)); // 'xslt' is the path to your xsl file

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now