【问题标题】:XML default namespace StaxEventItemWriter errorXML 默认命名空间 StaxEventItemWriter 错误
【发布时间】:2020-01-29 17:03:08
【问题描述】:

我有一个带有 StaxEventItemWriter 的 Spring 批处理应用程序。一切正常,但我需要将xmlns="http://www.demandware.com/1212" 添加到根元素。 当我创建地图并使用链接添加属性 xmlns 并将其设置为 .rootElementAttributes(attrs) 时,我在生成 xml 时出现异常

Caused by: javax.xml.stream.XMLStreamException: xmlns has been already bound to . Rebinding it to http://www.demandware.com/1212 is an error

如何添加默认命名空间?

【问题讨论】:

  • 下面提供的解决方案是否解决了您的问题?它没有解决我的问题。
  • 我的都不行! @truekiller 你有其他解决这个问题的方法吗?

标签: xml spring-batch stax


【解决方案1】:

我需要将 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 测试。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-10-18
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 2013-06-23
相关资源
最近更新 更多