假设有一个User.xml文件,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<user>
<id>1</id>
<name>lzj</name>
<age>28</age>
</user>
<user>
<id>2</id>
<name>Tom</name>
<age>20</age>
</user>
<user>
<id>3</id>
<name>Jerry</name>
<age>18</age>
</user>
<user>
<id>4</id>
<name>Jerry</name>
<age>18</age>
</user>
<user>
<id>5</id>
<name>Bob</name>
<age>25</age>
</user>
<user>
<id>5</id>
<name>Ferry</name>
<age>30</age>
</user>
<user>
<id>6</id>
<name>Marry</name>
<age>39</age>
</user>
<user>
<id>7</id>
<name>Linda</name>
<age>20</age>
</user>
<user>
<id>8</id>
<name>Linda</name>
<age>23</age>
</user>
<user>
<id>9</id>
<name>Jiken</name>
<age>15</age>
</user>
……
</root>
如果这个文件足够大,现在要在批处理程序中批量读取,首先建立对应的java bean
//@XmlRootElement(name = "user") //指定根元素
public class User implements Serializable{
private String id;
private String name;
private String age;
//省略set、get、toString方法
}
批处理程序程序如下:
@Configuration
public class BatchConfig {
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Value("${spring.batch.chunk.size:3}")
private int chunkSize;
/*1、创建一个Job作业*/
@Bean
public Job xmlReaderJob(){
return jobBuilderFactory.get("xmlReaderJob")
.start(chunkStep())
.build();
}
//2、创建一个step*/
@Bean
public Step chunkStep(){
return stepBuilderFactory.get("chunkStep")
.chunk(chunkSize) //每chunkSize次提交一次
.reader(xmlItemReader()) //读取xml文件,并把文件中每个标签中数据映射到工程中的User bean中
.writer(list -> list.forEach(System.out::println))
.allowStartIfComplete(true)
.build();
}
//3、配置要读取文件的特性*/
@Bean
public ItemReader<User> xmlItemReader(){
StaxEventItemReader<User> reader = new StaxEventItemReader<>(); //StaxEventItemReader用来读取xml文件
reader.setResource(new ClassPathResource("/data/User.xml")); //设置xml文件位置
reader.setFragmentRootElementName("user"); //指定xml文件的根元素,或者在User.java类上用@XmlRootElement(name = "user")
reader.setUnmarshaller(getMarShaller()); //把xml文件中数据映射到User.java中
// reader.setUnmarshaller(getJaxbMarShaller()); //未实现
return reader;
}
private Jaxb2Marshaller getJaxbMarShaller(){
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(User.class);
return marshaller;
}
private XStreamMarshaller getMarShaller(){
XStreamMarshaller marShaller = new XStreamMarshaller();
Map<String, Class> map = new HashMap<>();
map.put("user", User.class); //把<user>标签中数据映射到User.class类中
marShaller.setAliases(map);
return marShaller;
}
}
程序中应用到了把xml转化为java bean的技术,需要将spring-oxm包,需导入工程中
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
运行批处理程序,会打印出如下结果:
……
User [id=1, name=lzj, age=28]
User [id=2, name=Tom, age=20]
User [id=3, name=Jerry, age=18]
User [id=4, name=Jerry, age=18]
User [id=5, name=Bob, age=25]
User [id=5, name=Ferry, age=30]
User [id=6, name=Marry, age=39]
User [id=7, name=Linda, age=20]
User [id=8, name=Linda, age=23]
User [id=9, name=Jiken, age=15]
……