2.7.5. Writing an XML Document with StAX
In the preceding section, you saw how to produce an XML document by writing a DOM tree. If you have no other use for the DOM tree, that approach is not very efficient.
The StAX API lets you write an XML tree directly. Construct an
XMLStreamWriter from an
XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = factory.createXMLStreamWriter(out);
To produce the XML header, call
Add attributes by calling
Now you can add child elements by calling
writeStartElement again, or write characters with
When you ...