从XML读取的Spring批处理写入CSV文件

时间:2019-05-28 16:13:40

标签: spring-batch

嗨,我有一个春季批处理作业,可以从xml文件读取并写入Csv文件,但我有一个问题要更改此工作示例与带有嵌套标签的xml文件一起使用:

在工作示例xml中的

是:

<?xml version="1.0" encoding="UTF-8" ?>
<company>
    <record >
        <name>ben</name>
        <age>31</age>
        <dob>31/8/1982</dob>
        <income>200,000</income>
    </record>
    <record >
        <name>aya</name>
        <age>30</age>
        <dob>26/7/1983</dob>
        <income>100,999</income>
    </record>
   </company>

我希望它可以与xml文件一起使用:

<company>
    <record >
        <name>ben</name>
        <age>31</age>
        <dob>31/8/1982</dob>
        **<income>
           <Gross> 2000<Gross/>
           <Net>1000</Net>
        </income>**
    </record>
    <record >
        <name>aya</name>
        <age>30</age>
        <dob>26/7/1983</dob>
        <income>100,999</income>
    </record>
   </company>

  <batch:job id="reportJob">
    <batch:step id="step1">
    <batch:tasklet>
        <batch:chunk reader="xmlItemReader" 
            writer="cvsFileItemWriter" processor="filterReportProcessor"
            commit-interval="1">
        </batch:chunk>
    </batch:tasklet>
    </batch:step>
  </batch:job>

  <!-- Filtering process -->
  <bean id="filterReportProcessor" class="com.mkyong.processor.FilterReportProcessor" />

  <bean id="xmlItemReader" 
        class="org.springframework.batch.item.xml.StaxEventItemReader">
    <property name="fragmentRootElementName" value="record" />
    <property name="resource" value="classpath:xml/report.xml" />
    <property name="unmarshaller" ref="reportUnmarshaller" />
  </bean>

  <!-- Read and map values to object, via jaxb2 -->
  <bean id="reportUnmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
    <list>
        <value>com.ben.model.Report</value>
    </list>
    </property>
  </bean>

  <bean id="cvsFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
    <!-- write to this csv file -->
    <property name="resource" value="file:cvs/report.csv" />
    <property name="shouldDeleteIfExists" value="true" />

    <property name="lineAggregator">
      <bean
        class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
        <property name="delimiter" value="," />
        <property name="fieldExtractor">
          <bean
            class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
            <property name="names" value="refId, name, age, csvDob, income" />
           </bean>
        </property>
       </bean>
    </property>
  </bean>

</beans>

POJO是这样的:

@XmlRootElement(name = "record")
public class Report {

    private int refId;
    private String name;
    private int age;
    private Date dob;
    private BigDecimal income;

    @XmlAttribute(name = "refId")
    public int getRefId() {
        return refId;
    }

    public void setRefId(int refId) {
        this.refId = refId;
    }

    @XmlElement(name = "age")
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlJavaTypeAdapter(JaxbDateAdapter.class)
    @XmlElement
    public Date getDob() {
        return dob;
    }

    @XmlJavaTypeAdapter(JaxbDateAdapter.class)
    public void setDob(Date dob) {
        this.dob = dob;
    }

    @XmlJavaTypeAdapter(JaxbBigDecimalAdapter.class)
    @XmlElement
    public BigDecimal getIncome() {
        return income;
    }

    public void setIncome(BigDecimal income) {
        this.income = income;
    }

    // for csv demo only
    public String getCsvDob() {

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        return dateFormat.format(getDob());

    }

}

到目前为止,我试图在记录中创建一个收入类,并在XML配置文件中添加Gross和Net,如下所示:<property name="names" value="refId, name, age, csvDob, Gross,Net" />,但这没用,我没有得到Gross和Net的值。我应该怎么做才能使其正常工作。

1 个答案:

答案 0 :(得分:0)

这是一个JAXB问题,而不是Spring Batch问题。您的问题是如何映射此XML代码段:

else

<record > <name>ben</name> <age>31</age> <dob>31/8/1982</dob> <income> <Gross> 2000<Gross/> <Net>1000</Net> </income> </record> 类型的域对象。如您的示例中所定义,映射不起作用,因为字段Report的类型为income

  

到目前为止,我尝试创建一个收入类

您需要将BigDecimal字段类型从income更新为BigDecimal