How to write XML file in Java using Java StAX Iterator API With Examples

Java Streaming API for XML or Java StAX API was introduced in Java 6 and considered superior to DOM and SAX parsers. As explained in earlier post about Java StAX API, it consists of cursor-based API and iterator based API.

Here we will see how we can write XML file in java using StAX Iterator based API (XMLEventWriter).

Above program creates below XML file:


I have used n and t elements to create the formatted XML but to save memory and reduce processing time, you can remove them from the implementation.

Also for debugging purpose, you can change the output stream to System.out to print the XML file to console.

By admin

Leave a Reply

%d bloggers like this: