How to convert XMLStreamReader to XMLStreamWriter

It should be easy and clear, but I can’t find a way - it XMLOutputFactorytakes anly OutputStream, Resultor another Writerto create a new one XMLStreamWriter.

What I have on hand is XMLStreamReaderthat has no methods for extracting a Resultor OutputStream.

If the solution was simpler using the Event API, that would be nice too.

thank

+5
source share
1 answer

You can use javax.xml.transform.Transformerto convert a StAXSourcewrapping the reader to StAXResult, wrapping the author.

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 can also use the following command:

+10

All Articles