views:

31

answers:

4

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?

A: 

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.

Jim Garrison
A: 

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

StaxMan
A: 

@Laird:

I used following code to indicate empty elements should be generated as <x/>

XMLOutputFactory factory = XMLOutputFactory2.newInstance();
factory.setProperty(XMLOutputFactory2.P_AUTOMATIC_EMPTY_ELEMENTS, Boolean.TRUE);

But I receive following error:

java.lang.IllegalArgumentException: Property org.codehaus.stax2.automaticEmptyElementsis not supported
    at com.sun.xml.internal.stream.XMLOutputFactoryImpl.setProperty(Unknown Source)
    at ch.synlogic.iaf.export.IAFExporter.processElementDataPerType(IAFExporter.java:103)
    at ch.synlogic.iaf.export.IAFExporter.export(IAFExporter.java:73)
    at ch.synlogic.income.domain.export.ExportIncomeRepositoryApp.exportIncomeRepository(ExportIncomeRepositoryApp.java:106)
    at ch.synlogic.income.domain.export.ExportIncomeRepositoryApp.runApp(ExportIncomeRepositoryApp.java:89)
    at ch.synlogic.income.core.runtime.IncomeRuntimeApplication.start(IncomeRuntimeApplication.java:32)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:193)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:386)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:549)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1236)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1212)
thequark
A: 

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

thequark