мне нужно добавить xmlns = "http://www.demandware.com/1212" к корневому элементу
Вот пример того, как добавить пользовательское пространство имен к корневому элементу:
import java.util.Arrays;
import java.util.HashMap;
import javax.xml.bind.annotation.XmlRootElement;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@Configuration
@EnableBatchProcessing
public class MyJob {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
public ItemReader<Person> itemReader() {
return new ListItemReader<>(Arrays.asList(
new Person(1, "foo"),
new Person(2, "bar"))
);
}
@Bean
public ItemWriter<Person> itemWriter() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(Person.class);
HashMap<String, String> rootElementAttributes = new HashMap<String, String>() {{
put("xmlns", "http://www.demandware.com/1212");
}};
return new StaxEventItemWriterBuilder<Person>()
.name("personWriter")
.resource(new FileSystemResource("persons.xml"))
.marshaller(marshaller)
.rootTagName("persons")
.rootElementAttributes(rootElementAttributes)
.build();
}
@Bean
public Step step() {
return steps.get("step")
.<Person, Person>chunk(5)
.reader(itemReader())
.writer(itemWriter())
.build();
}
@Bean
public Job job() {
return jobs.get("job")
.start(step())
.build();
}
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}
@XmlRootElement
public static class Person {
private int id;
private String name;
public Person(int id, String name) {
this.id = id;
this.name = name;
}
public Person() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
Генерирует следующее persons.xml
:
<?xml version='1.0' encoding='UTF-8'?>
<persons xmlns="http://www.demandware.com/1212">
<person>
<id>1</id>
<name>foo</name>
</person>
<person>
<id>2</id>
<name>bar</name>
</person>
</persons>
Протестировано с Spring Batch v4.1.2.