如何从MULTIPLE XML文件中提取数据并将其放在Spring批处理的一个XML文件中?

时间:2014-10-20 10:00:13

标签: java xml spring-batch

我已经生成了与我的xml文件相对应的java类(通过jaxb工具)。

然后我编写了配置文件,如下所示:

<batch:job id="bghJob" parent="simpleJob">

    <batch:step id="step1">
        <batch:tasklet>
            <batch:chunk reader="multiResourceReader" writer="xmlItemWriter"
                commit-interval="1" />
        </batch:tasklet>
    </batch:step>

</batch:job>


<!-- MULTI Reader -->
<bean id="multiResourceReader"
    class=" org.springframework.batch.item.file.MultiResourceItemReader">
    <property name="resources" value="classpath:xmls/*.xml" />
    <property name="delegate" ref="xmlItemReader" />
</bean>

<!-- Reader -->
<bean id="xmlItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
    <property name="fragmentRootElementName" value="T_Document" />
    <property name="unmarshaller" ref="invoiceUnMarshaller" />
</bean>

<!-- Unmarshaller -->
<bean id="invoiceUnMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="contextPath" value="com.xxx.model.inputJaxB" />
</bean>

<!-- Writer -->
<bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
    <property name="resource" value="file:xml/outputs/Facture.xml" />
    <property name="marshaller" ref="invoiceMarshaller" />
    <property name="rootTagName" value="Facture" />
</bean>

<!-- Marshaller -->
<bean id="invoiceMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.xxx.model.output.Facture</value>
        </list>
    </property>
</bean>

,这是我的App.java:

public class App {

public static long customerID = 201832;

public static void main(String[] args) {

    String[] springConfig = { "spring/batch/config/context.xml", "spring/batch/jobs/job-bgh.xml" };

    @SuppressWarnings("resource")
    ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);

    JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
    Job job = (Job) context.getBean("bghJob");

    try {

        JobExecution execution = jobLauncher.run(job, new JobParameters());
        System.out.println("Exit Status : " + execution.getStatus());

    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("Done");

}

}

我的问题是创建的xml输出文件几乎为空。这是:

<?xml version="1.0" encoding="UTF-8"?>
<Facture></Facture>

我错过了什么?为什么我的输出xml为空?


修改

我的Facture.java类输出xml:

@XmlRootElement(name = "T_Document")

@XmlAccessorType(XmlAccessType.FIELD) 公共阶层事实{

@XmlAttribute(name = "Sender", required = true)
private String sender;

@XmlAttribute(name = "Id", required = true)
private String id;

@XmlAttribute(name = "BAId", required = true)
private String baId;

public String getSender() {
    return sender;
}

public void setSender(String value) {
    this.sender = value;
}

public String getId() {
    return id;
}

public void setId(String value) {
    this.id = value;
}

public String getBAId() {
    return baId;
}

public void setBAId(String value) {
    this.baId = value;
}

}

0 个答案:

没有答案