spring-batch icon indicating copy to clipboard operation
spring-batch copied to clipboard

StaxEventItemWriter fails to create formatted output [BATCH-1835]

Open spring-projects-issues opened this issue 13 years ago • 9 comments

Oliver Theissen opened BATCH-1835 and commented

A Jaxb2Marshaller used in a StaxEventItemWriter fails to create formatted XML output (one element per line, ident lines) even if the Jaxb2Marshaller has been configured to do so. See the sample application (STS project, maven build) to reproduce: this application reads some sample data from a csv file in data/in and creates two XML files in the directory data/out: one XML file created by a StaxEventItemWriter and the other created by a "home made" XmlItemWriter. Both ItemWriters use the same Jaxb2Marshaller bean for marshalliung but produce different output: the output of the StaxEventItemWriter fails to be formatted.


Affects: 2.1.8

Reference URL: http://forum.springsource.org/archive/index.php/t-69570.html

Attachments:

4 votes, 10 watchers

spring-projects-issues avatar Feb 05 '12 13:02 spring-projects-issues

David J. M. Karlsen commented

Any chance of getting this fixed?

spring-projects-issues avatar May 13 '13 07:05 spring-projects-issues

Jimmy Praet commented

There is no standard support for XML formatting in stax. But a third-party library called stax-utils (https://java.net/projects/stax-utils/) does provide this functionality (with javanet.staxutils.IndentingXMLEventWriter).

If you really need this you could extend StaxEventItemWriter and override the createXmlEventWriter method to return an IndentingXMLEventWriter.

spring-projects-issues avatar May 13 '13 10:05 spring-projects-issues

David J. M. Karlsen commented

Yes, this is what I ended up with if the project wants it:

+import java.io.Writer;
+
+import javanet.staxutils.IndentingXMLEventWriter;
+
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+
+import org.springframework.batch.item.xml.StaxEventItemWriter;
+
+
+public class ExtendedStaxEventItemWriter<T>
+    extends StaxEventItemWriter<T>
+{
+    
+    public void setIndenting( boolean indenting )
+    {
+        this.indenting = indenting;
+    }
+    
+    public boolean isIndenting()
+    {
+        return indenting;
+    }
+    
+    @Override
+    protected XMLEventWriter createXmlEventWriter( XMLOutputFactory outputFactory, Writer writer )
+        throws XMLStreamException
+    {
+        if ( isIndenting() ) {
+            return new IndentingXMLEventWriter( super.createXmlEventWriter( outputFactory, writer ) );
+        }
+        else {
+            return super.createXmlEventWriter( outputFactory, writer );
+        }
+    }
+
+}

spring-projects-issues avatar May 13 '13 10:05 spring-projects-issues

Sebastien Lorber commented

Check also this issue: https://jira.springsource.org/browse/BATCH-2113

spring-projects-issues avatar Sep 20 '13 08:09 spring-projects-issues

Smail Louahchi commented

Please check, if its possible to add prettyPrint Feature to StaxEventItemWriter with IndentingXMLEventWriter.

In my CustomStaxEventItemWriter the user has the choice with an boolean flag for pretty print. The close method only requires an CR/LF for the end tag in mode 'prettyPrint'.

<dependency>
	<groupId>net.java.dev.stax-utils</groupId>
	<artifactId>stax-utils</artifactId>
	<version>20040917</version>
</dependency>
private boolean prettyPrint = false;
...
public void setPrettyPrint(boolean prettyPrint)
{
	this.prettyPrint = prettyPrint;
}
...
protected XMLEventWriter createXmlEventWriter(XMLOutputFactory outputFactory, Writer writer) throws XMLStreamException
{
	XMLEventWriter result = outputFactory.createXMLEventWriter(writer);
	if (prettyPrint)
	{
		return new IndentingXMLEventWriter(result);
	}
	return result;
}
...
public void close() {
	super.close();

	XMLEventFactory factory = createXmlEventFactory();
	try {
		delegateEventWriter.add(factory.createCharacters(prettyPrint ? "\n" : ""));
	}
...
<bean id="staxItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
	...
	<property name="prettyPrint" value="true" />
</bean>

spring-projects-issues avatar Oct 06 '14 06:10 spring-projects-issues

Ketan Prajapati commented

@Smail, thanks for possible solution. It is helping to generate pretty printed XML but it has a side effect as mentioned in https://jira.springsource.org/browse/BATCH-2113. because StaxEventItemWriter is not working fine when using IndentingXMLEventWriter.

When I am trying hacks mentioned in https://jira.springsource.org/browse/BATCH-2113 along with your solution, it is working fine for me but had to leave restart ability on risk which is not ideal solution.

spring-projects-issues avatar Nov 04 '16 15:11 spring-projects-issues

Vishal Pawar commented

Thanks all for the info on this issue. Do we have a better solution for this since last discussed

spring-projects-issues avatar Aug 30 '18 20:08 spring-projects-issues

Vishal Pawar commented

Thanks all for the info on this issue. Do we have a better solution for this since last discussed

I'm wondering the same thing as I recently ran into this exact same issue.

GregBragg avatar Aug 21 '23 16:08 GregBragg

Smail Louahchi commented

Please check, if its possible to add prettyPrint Feature to StaxEventItemWriter with IndentingXMLEventWriter.

In my CustomStaxEventItemWriter the user has the choice with an boolean flag for pretty print. The close method only requires an CR/LF for the end tag in mode 'prettyPrint'.

<dependency>
	<groupId>net.java.dev.stax-utils</groupId>
	<artifactId>stax-utils</artifactId>
	<version>20040917</version>
</dependency>

The dependency to the Bea jar is no longer in the Maven repo, however it is not needed with the latest JDKs as the dependent APIs are already included. This works for me for the code that Sebastien Lorber provided in https://github.com/spring-projects/spring-batch/issues/1484.

        <dependency>
            <groupId>net.java.dev.stax-utils</groupId>
            <artifactId>stax-utils</artifactId>
            <version>20070216</version>
            <exclusions>
                <exclusion>
                    <groupId>com.bea.xml</groupId>
                    <artifactId>jsr173-ri</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

GregBragg avatar Aug 21 '23 16:08 GregBragg