6

Should be easy and obvious but I cant find a way - the XMLOutputFactory accepts anly OutputStream, Result or another Writer to generate a new XMLStreamWriter.

What I have at hand is an XMLStreamReader which has no methods for extracting a Result or an OutputStream.

If the solution would be easier using the Event API, that would be OK too.

Thank you

kostja
  • 60,521
  • 48
  • 179
  • 224

1 Answers1

11

You could use a javax.xml.transform.Transformer to convert a StAXSource wrapping the reader to a StAXResult wrapping the writer.

TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
StAXSource source = new StAXSource(xmlStreamReader);
StAXResult result = new StAXResult(xmlStreamWriter);
t.transform(source, result);

Using the Event API you could also use the folloiwng:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 1
    @kostja - You may also be interested in the following method: http://download.oracle.com/javase/6/docs/api/javax/xml/stream/XMLEventWriter.html#add(javax.xml.stream.XMLEventReader) – bdoughan Apr 05 '11 at 15:04
  • thank you, that's what I ended up using - the add() - methods of the `XMLEventWriter` are so much more comfortable and natural to use. – kostja Apr 06 '11 at 09:26
  • Could you please show or enhance the answer showing how to use `XMLEventWriter.add(XMLEventReader)` for the above purpose? – Somu Apr 22 '14 at 14:14