Friday, May 6, 2011

How to 'transform' a String object (containing XML) to an element on an existing JSP page

Currently I have a String object that contains XML elements:

String carsInGarage = garage.getCars();

I now want to pass this String as an input/stream source (or some kind of source), but am unsure which one to choose and how to implement it.

Most of the solutions I have looked at import the package: javax.xml.transform and accept a XML file (stylerXML.xml) and output to a HTML file (outputFile.html) (See code below).

try 
{
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer 
        (new javax.xml.transform.stream.StreamSource("styler.xsl"));

    transformer.transform (
        new javax.xml.transform.stream.StreamSource ("stylerXML.xml"),
        new javax.xml.transform.stream.StreamResult (
            new FileOutputStream("outputFile.html")));
}
catch (Exception e)
{
e.printStackTrace();
}

I want to accept a String object and output (using XSL) to a element within an existing JSP page. I just don't know how to implement this, even having looked at the code above.

Can someone please advise/assist. I have searched high and low for a solution, but I just can't pull anything out.

Thankyou, Lucas

From stackoverflow
  • Use a StringReader and a StringWriter:

     try {
      StringReader reader = new StringReader("<xml>blabla</xml>");
      StringWriter writer = new StringWriter();
      TransformerFactory tFactory = TransformerFactory.newInstance();
      Transformer transformer = tFactory.newTransformer(
        new javax.xml.transform.stream.StreamSource("styler.xsl"));
    
      transformer.transform(
        new javax.xml.transform.stream.StreamSource(reader), 
        new javax.xml.transform.stream.StreamResult(writer));
    
      String result = writer.toString();
     } catch (Exception e) {
      e.printStackTrace();
     }
    
    Lycana : Hi Bruno, Thank you for that excellent answer. It is compiling properly now :) However, I still am unable to print it to the screen. I am currently trying: out.println(writer); but this is failing.
    bruno conde : Maybe there was an error with the transform ... how exactly is it failing?
    Lycana : It fails as nothing is printed to the browser. At the bottom of my try block I have: String result = writer.toString(); out.print("
    " + result + "
    "); I also just installed a tool called Firebug to inspect the content of my HTML body. Nothing again.
    Lycana : I don't know if this has anything to do with it, but I can out.print() a random string outside of the TRY-block. But I cannot out.print() the same random string when it is inside the TRY-block.
    Lycana : Hi Bruno, I have managed to get the transformed/styled XML output to screen. I must thank you for your assistance. You are amazing :) I do have one question left though :P Did you expect the original string AS WELL as the transformed/styled XML to output together? Or did you just expect the transformed/styled only to show?
    bruno conde : No. Only the transformed/styled result is in the writer. If your getting strange results check your XSL.
  • If at some point you want the source to contain more than just a single string, or you don't want to generate the XML wrapper element manually, create a DOM document that contains your source and pass it to the transformer using a DOMSource.

  • Hey Jordan, I am having the same problem as you had with the browser display being blank. I was just wondering how you got the result to appear on the screen? Thanks

0 comments:

Post a Comment