Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform XMLStreamReader to XMLStreamWriter

Tags:

java

io

xml

stax

sax

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

like image 271
kostja Avatar asked Apr 05 '11 09:04

kostja


1 Answers

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:

  • http://download.oracle.com/javase/6/docs/api/javax/xml/stream/XMLEventWriter.html#add(javax.xml.stream.XMLEventReader)
like image 59
bdoughan Avatar answered Nov 10 '22 01:11

bdoughan