XMLEventWriter: how can I tell it to write empty elements?
Asked Answered
C

5

10

I do not see an option within javax.xml.stream.XMLEventWriter or javax.xml.stream.XMLOutputFactory to set either up in a way so that empty elements are written (instead of explicit start and end element pairs).

I see that Woodstox has a property to do this, but it is not standardized.

Am I missing any obvious way to do this?

Closure answered 29/6, 2010 at 15:51 Comment(2)
<x></x> is an empty element.Coolidge
Of course you are quite right. I should have been more specific, since I obviously wasn't clear here. What I mean is, an element that ends with "/>".Closure
M
3

No. There is no semantic difference between <x/> and <x></x> and the standard APIs do not provide a way to request one or the other.

Maas answered 29/6, 2010 at 15:59 Comment(3)
An implementation detail of the StAX writer included with the JDK by default: calling writeStartElement("localname");writeEndElement() results in a self-closing tag, while calling writeStartElement("localname");writeCharacters(null);writeEndElement() results in an opening tag immediately followed by a closing tag.Latoyalatoye
As you note, that is an implementation detail. Other serializers and later versions of StAX may or may not do this.Maas
I just have an issue where JAXB chokes on <xi:include href="somePatch/someXml"></xi:include> but is fine with <xi:include href="somePatch/someXml"/>. So there seems to be a semantic difference? Or is it a bug in JAXB?Aretha
U
7
writer.writeEmptyElement("some_element");
writer.writeAttribute("some_attribute", "some_value");
Underground answered 20/3, 2013 at 14:44 Comment(0)
L
4

Setting property so that empty tags are generated like <x/> works with WoodStox APIs:

WstxOutputFactory factory = new WstxOutputFactory();
factory.setProperty(WstxOutputFactory.P_AUTOMATIC_EMPTY_ELEMENTS, true);

I wanted indentation in XML tags. the setIndentation method is working with neither javax.xml.stream.XMLOutputFactory nor org.codehaus.stax2.XMLOutputFactory2

Lively answered 14/7, 2010 at 16:15 Comment(0)
M
3

No. There is no semantic difference between <x/> and <x></x> and the standard APIs do not provide a way to request one or the other.

Maas answered 29/6, 2010 at 15:59 Comment(3)
An implementation detail of the StAX writer included with the JDK by default: calling writeStartElement("localname");writeEndElement() results in a self-closing tag, while calling writeStartElement("localname");writeCharacters(null);writeEndElement() results in an opening tag immediately followed by a closing tag.Latoyalatoye
As you note, that is an implementation detail. Other serializers and later versions of StAX may or may not do this.Maas
I just have an issue where JAXB chokes on <xi:include href="somePatch/someXml"></xi:include> but is fine with <xi:include href="somePatch/someXml"/>. So there seems to be a semantic difference? Or is it a bug in JAXB?Aretha
M
3

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"));
Mancilla answered 21/4, 2015 at 14:23 Comment(0)
N
2

You probably know this already, but XMLStreamWriter does have method for specifying that it should be "real" empty element. XMLEventWriter is missing a few pieces that lower level interface has.

Negligent answered 10/7, 2010 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.