Search code examples
springspring-batchjaxb2

Complex XML using Spring Batch; StaxEventItemWriter ; Jaxb2Marshaller


I need to write a slightly complex XML using Spring Batch. Can anyone please help me with the appropriate Spring configuration?

Below is the Output the process requires.

<XML>
<USERLIST ID="something" NAME="Sample">
  <USER ID="userID" NAME="Name"/>
  <USER ID="userID" NAME="Name"/>
  ........
</USERLIST>
<XML>

The 'UserList' in the XML above only needs to occur once

This is the spring configuration I have so far.

<bean id="userXMLWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
    <property name="resource" value="file:outputs/users.xml" />
    <property name="encoding" value="ISO-8859-1" />
    <property name="version" value="1.0" />
    <property name="marshaller" ref="userXMLMarshaller" />
    <property name="rootTagName" value="XML" />
  </bean>

  <bean id="userXMLMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  <property name="marshallerProperties">
        <map>
            <entry>
                <key>
                    <util:constant static-field="javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT" />
               </key>
              <value type="java.lang.Boolean">true</value>
            </entry>
        </map>
    </property>
    <property name="classesToBeBound">
    <list>
        <value>org.test.model.xml.UserList</value>
        <value>org.test.model.xml.User</value>
    </list>
    </property>
  </bean>   

Obviously, when I test this my XML does not have the 'USERLIST' element in it because all these USER objects need to be added to the USERLIST somewhere. I am kinda new to Spring Batch and JAXB2. Any ideas on this is appreciated.

Thanks, Harish


Solution

  • The solution to this problem is to implement Header/Footer Callback classes provided by Spring (As Michael Minella suggested in the comments below.), StaxWriterCallback class in my case. Below is How I've implemented it.

    <bean id="userXMLWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
        <property name="resource" value="file:outputs/users.xml" />
        <property name="encoding" value="ISO-8859-1" />
        <property name="version" value="1.0" />
        <property name="marshaller" ref="userXMLMarshaller" />
        <property name="headerCallback" ref="UserXMLHeaderCallBack" />
        <property name="footerCallback" ref="UserXMLFooterCallBack"/>
        <property name="rootTagName" value="XML" />
     </bean>
    
    <bean id="UserXMLHeaderCallBack" class ="org.test.writers.UserXMLHeaderCallBack"/>
    <bean id="UserXMLFooterCallBack" class ="org.test.writers.UserXMLFooterCallBack"/>
    
      <bean id="userXMLMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
         <property name="classesToBeBound">
         <list>
           <value>org.test.model.xml.User</value>
        </list>
        </property>
      </bean>  
    

    And below are the Footer/Header callback classes implementation

     public class UserXMLHeaderCallBack implements StaxWriterCallback{  
          @Override
            public void write(XMLEventWriter writer) throws IOException {
                try{
                XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    
                Attribute id = eventFactory.createAttribute("ID", "someId");
                Attribute name = eventFactory.createAttribute("NAME", "someName");
                List<Attribute> attributeList = Arrays.asList(id, name);
                List<?> nsList = Arrays.asList();
    
                XMLEvent event = eventFactory.createStartElement("", "", "USERLIST",attributeList.iterator(), nsList.iterator());
                writer.add(event);
    
                }catch(XMLStreamException e){
                    System.err.println("Something went nuts!!!");
                }
            }
    
        }
    

    Footer class

     public class UserXMLFooterCallBack implements StaxWriterCallback{
    
        @Override
        public void write(XMLEventWriter writer) throws IOException {
            try{
                XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    
                XMLEvent event = eventFactory.createEndElement("", "", "USERLIST");
                writer.add(event);
            }catch(XMLStreamException e){
                System.err.println("Something went nuts!!!");
            }
        }
    
    }
    

    And I got the desired Output!