0

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.

Community
  • 1
  • 1
Shishigami
  • 441
  • 3
  • 7
  • 20

4 Answers4

1

Looks like what you really want to do is take some/all of the fields from the resultset and write it to a known output format.

Depending on the complexity of the data that you want to output, instead of using XSL what you could also do is get the data from the resultset into Java's Collection objects, and pass that data to template engines like Velocity or FreeMarker.

Your Velocity/Freemarker template would be (more of less) in the format of your intended output, and would thus be easier to maintain.

On the down side, if the data in your resultset is fairly rich, your data structures would become complex, and you'd want to create JavaBeans to represent the data before passing it onto the template engine. This would be more work, but you need to balance it against the effort of writing and maintaining XSL.

Rajesh J Advani
  • 5,585
  • 2
  • 23
  • 35
0

You could have the webRowSet write its XML to a ByteArrayOutputStream (preferably wrapped inside a BufferedOutputStream for buffering purposes).

Next, you can instruct your Transformer to read from a ByteArrayInputStream, which you instantiate with the byte[] you obtain from the ByteArrayOutputStream.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
0

You have following steps:
WebRowSet writes data as xml to output stream --> you convert it somehow to input stream --> you pass input stream to transformer and it writes result to response

The second step is tricky. One of the ways to achieve is to write to file as you mentioned. You can also write it in memory using ByteArrayOutputStream:

ByteArrayOutputStream tempStream = new ByteArrayOutputStream();
webRowSet.writeXml(resulSet, tempStream);
...
transformer.transform(new StreamSource(new ByteArrayInputStream(tempStream.toByteArray())),
                      new StreamResult(response.getWriter()));

It should work unless you have large xml that can't fit into memory. In this way you can either write to file or look for solution that doesn't write in memory.

Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
0

Just a quick note to your question concerning the streams: web-servers also use streams to write the data, as you can see in the last line of your code.

The Output-Stream the transformation-object writes to, is the response stream.

ovm
  • 2,452
  • 3
  • 28
  • 51