Woodstox : Generate XML Using StAX

Overview

Woodstox StAX Streaming API can be used to generate XML document.

The key classes responsible for generating XML content are
XMLOutputFactory2
and
XMLStreamWriter2
.

Create TestGenerateXml class as shown below.

Point the outstream to Console output (see line 16 below).

The following methods are used for XML content generation:

  • create start element using writeStartElement() method (e.g. see line 20 below)
  • create element content using writeCharacters() method (e.g. see line 22 below)
  • create end element using writeEndElement() method (e.g. see line 23 below)

Finally flush the stream writer to print the output on console (see line 37 below).

Note that Woodstox shall automatically escape the special character ‘&’ (see line 34 below) when generating XML content.

The output of the program demonstrating XML generation is shown below. In particular, note that the special character ‘&’ in address has been escaped automatically by Woodstox.

Download Source Code for this Program

Leave a Comment