gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:23 24 4
gpt4 key购买 nike

我正在尝试读取一个 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>

最佳答案

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

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

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

关于java - 在Spring Batch中,如何读取XML属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49161020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com