使用Spring Batch的复杂XML; StaxEventItemWriter; Jaxb2Marshaller

时间:2014-08-04 23:44:36

标签: spring spring-batch jaxb2

我需要使用Spring Batch编写稍微复杂的XML。有谁能帮助我使用适当的Spring配置?

以下是流程所需的输出。

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

&#39; UserList&#39;在上面的XML中只需要发生一次

这是我到目前为止的弹簧配置。

<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>   

显然,当我测试这个时,我的XML没有&#39; USERLIST&#39;因为所有这些USER对象都需要在某处添加到USERLIST中。我是Spring Batch和JAXB2的新手。对此有任何想法表示赞赏。

谢谢, 哈里什

1 个答案:

答案 0 :(得分:4)

解决这个问题的方法是实现Spring提供的Header / Footer Callback类(正如Michael Minella在下面的评论中所建议的那样<。strong>。),在我的案例中是StaxWriterCallback类。以下是我如何实施它。

<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>  

以下是Footer / Header回调类实现

 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!!!");
            }
        }

    }

页脚类

 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!!!");
        }
    }

}

我得到了所需的输出!