Spring Batch
  1. Spring Batch
  2. BATCH-1835

StaxEventItemWriter fails to create formatted output

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.1.8
    • Fix Version/s: None
    • Component/s: Infrastructure
    • Labels:
      None
    • Environment:
      JDK 1.6, Windows 7

      Description

      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.

        Activity

        Hide
        David J. M. Karlsen added a comment -

        Any chance of getting this fixed?

        Show
        David J. M. Karlsen added a comment - Any chance of getting this fixed?
        Hide
        Jimmy Praet added a comment -

        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.

        Show
        Jimmy Praet added a comment - 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.
        Hide
        David J. M. Karlsen added a comment -

        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 );
        +        }
        +    }
        +
        +}
        
        Show
        David J. M. Karlsen added a comment - 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 ); + } + } + +}
        Hide
        Sebastien Lorber added a comment -
        Show
        Sebastien Lorber added a comment - Check also this issue: https://jira.springsource.org/browse/BATCH-2113

          People

          • Assignee:
            Unassigned
            Reporter:
            Oliver Theissen
          • Votes:
            3 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated: