I’ve been working on a webservice that prints the content of a table as XML – I used a WebRowSet to do this.
response.setContentType("text/xml");
WebRowSet webRowSet = new WebRowSetImpl();
webRowSet.writeXml(resultSet, response.getWriter());
(HttpServletResponse response)
While this much works fine, I simply can’t figure out howto apply a Stylesheet to the XML. I tried to use a transformer to do this.
webRowSet.writeXml(resultSet, OutputStream);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(new StreamSource("file.xsl"));
transformer.transform(new StreamSource(InputStream),
new StreamResult(response.getWriter()));
I fail to understand what the OutputStream in writeXml and the InputStream in the StreamSource would be. (I don’t want to save the data in a file)
I’ve found a question that might be related?
An explanation of how I could solve this problem would be very appreciated.
You could have the
webRowSetwrite its XML to aByteArrayOutputStream(preferably wrapped inside aBufferedOutputStreamfor buffering purposes).Next, you can instruct your
Transformerto read from aByteArrayInputStream, which you instantiate with thebyte[]you obtain from theByteArrayOutputStream.