Description
Mahmoud Ben Hassine opened BATCH-2856 and commented
As of v4.2.0, the only way to add the standalone
attribute in the XML declaration of the output file of StaxEventItemWriter
is to extend the writer and override startDocument
(as stated in its javadoc). Here is an example:
@Bean
public StaxEventItemWriter<Person> itemWriter() {
StaxEventItemWriter<Person> itemWriter = new StaxEventItemWriter<Person>() {
@Override
protected void startDocument(XMLEventWriter writer) throws XMLStreamException {
writer.add(createXmlEventFactory().createStartDocument(DEFAULT_ENCODING, DEFAULT_XML_VERSION, true));
writer.add(xmlEventFactory.createStartElement(getRootTagNamespacePrefix(), getRootTagNamespace(), getRootTagName()));
}
};
// TODO set other properties on the writer
return itemWriter;
}
The StaxEventItemWriter
provides setters for the version
and encoding
attributes, but not for the standalone
attribute. I believe it can also provide a setter for standalone
as this will make it easier to customize the XML declaration rather than having to extend the writer and override startDocument
(which is error prone BTW if we forget to add the root element as shown in the example above).
NB: The example shown above works with Java 9+, see https://bugs.openjdk.java.net/browse/JDK-8139584
Affects: 4.2.0
Reference URL: https://stackoverflow.com/questions/58714744/staxeventitemwriter-xml-header