简体   繁体   中英

How to transform XMLStreamReader to XMLStreamWriter

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

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM