在Spring Batch中,如何读取XML属性?

时间:2018-03-07 20:54:48

标签: java xml spring

我有一个我试图阅读的XML文件,其中包含具有属性的元素。我尝试了多个示例,但是我的类中的字段总是以null结尾,如下所示:

Data [type=null, value=null]
Data [type=null, value=null]
Data [type=null, value=null]

以下是我的问题的缩减示例代码。

以下是位于src / main / resources / data目录(data.xml)中的示例XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<list>
    <data type="shopping" value="milk" />
    <data type="shopping" value="eggs" />
    <data type="TODO" value="return books to library" />
</list>

下面是我的XML数据(Data.java)的域类:

package com.example.demo.springbatchtest.domain;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "data")
public class Data {
    private final String type;
    private final String value;

    public Data(String type, String value) {
        this.type = type;
        this.value = value;
    }

    @XmlAttribute(name = "type")
    public String getType() {
        return type;
    }

    @XmlAttribute(name = "value")
    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "Data [type=" + type + ", value=" + value + "]";
    }

}

这是我的Spring Batch作业配置文件(JobConfiguration.java):

package com.example.demo.springbatchtest.configuration;

import java.util.HashMap;
import java.util.Map;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.xml.StaxEventItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.oxm.xstream.XStreamMarshaller;

import com.example.demo.springbatchtest.domain.Data;

@Configuration
public class JobConfiguration {
    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    StepBuilderFactory stepBuilderFactory;

    @Bean
    public StaxEventItemReader<Data> dataItemReader() {
        XStreamMarshaller unmarshaller = new XStreamMarshaller();

        Map<String, Class> aliases = new HashMap<>();
        aliases.put("data", Data.class);

        unmarshaller.setAliases(aliases);

        StaxEventItemReader<Data> reader = new StaxEventItemReader<>();

        reader.setResource(new ClassPathResource("/data/data.xml"));

        reader.setFragmentRootElementName("data");
        reader.setUnmarshaller(unmarshaller);

        return reader;
    }

    @Bean
    public ItemWriter<Data> dataItemWriter() {
        return items -> {
            for (Data item : items) {
                System.out.println(item.toString());
            }
        };
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .<Data, Data>chunk(10)
                .reader(dataItemReader())
                .writer(dataItemWriter())
                .build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory
                .get("job")
                .start(step1())
                .build();
    }

}

这是我的主要Spring Boot类(SpringBatchTestApplication.java):

package com.example.demo.springbatchtest;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBatchTestApplication.class, args);
    }
}

这是我的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>SpringBatchTestSpringBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringBatchTestSpringBoot</name>
    <description>Spring Batch Test with Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2 个答案:

答案 0 :(得分:0)

听起来JAX-B无法设置Data.type和value属性。

默认情况下,它会为每个属性寻找一个getter / setter对,否则它们将被视为只读。

另一种方法是使用字段级访问 - @XmlAccessorType(XmlAccessType.FIELD)

答案 1 :(得分:0)

发现本教程帮助我获得了示例代码。

https://walkingtechie.blogspot.com/2017/03/spring-batch-xml-file-to-mysql-example.html

关键是转换器类来解析属性。所以在我的例子中,我在JobConfiguration类的dataItemReader()中添加了一个调用set类。

import com.example.demo.springbatchtest.converter.DataConverter;

...

@Autowired
private DataConverter dataConverter;

...

@Bean
public StaxEventItemReader<Data> dataItemReader() {
    XStreamMarshaller unmarshaller = new XStreamMarshaller();

    Map<String, Class> aliases = new HashMap<>();
    aliases.put("data", Data.class);

    unmarshaller.setAliases(aliases);
    unmarshaller.setConverters(dataConverter);  // from Walking Techie

    StaxEventItemReader<Data> reader = new StaxEventItemReader<>();

    reader.setResource(new ClassPathResource("/data/data.xml"));

    reader.setFragmentRootElementName("data");
    reader.setUnmarshaller(unmarshaller);

    return reader;
}

然后我扩展了Converter类来处理属性。

package com.example.demo.springbatchtest.converter;

import org.springframework.stereotype.Component;

import com.example.demo.springbatchtest.domain.Data;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

@Component
public class DataConverter implements Converter {

  @Override
  public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {

  }

  @Override
  public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    String type = reader.getAttribute("type");
    String value = reader.getAttribute("value");

    return new Data(type, value);
  }


  @Override
  public boolean canConvert(Class type) {
    return type.equals(Data.class);
  }
}

该程序现在输出以下内容:

Data [type=shopping, value=milk]
Data [type=shopping, value=eggs]
Data [type=TODO, value=return books to library]