In several of the answers and comments there is some confusion.
StAX has two APIs:
- The "Cursor API" using
XMLStreamReader
and XMLStreamWriter
; and
- The "Iterator API" using
XMLEventReader
andXMLEventWriter
;
Outputting an empty element with a single tag, <example/>
, is possible with the Cursor API usingXMLStreamWriter
:
xmlStreamWriter.writeEmptyElement("example");
Outputting an empty element with a single tag, <example/>
, is not possible with the Iterator API using XMLEventWriter
, as far as I know. In this case you're stuck with producing an empty element with two tags <example></example>
:
xmlEventWriter.add(xmlEventFactory.createStartElement("", null, "example"));
xmlEventWriter.add(xmlEventFactory.createEndElement("", null, "example"));
<x></x>
is an empty element. – Coolidge