使用Spring Batch XML Writer,如何在特定标签中写入项目?

时间:2019-08-20 08:24:42

标签: spring-batch spring-batch-admin spring-oxm

我遇到了这样一种情况,我需要将处理后的项目以XML写入特定标签内。我正在将块模型与并行处理一起使用。有没有办法在并行处理中实现这一目标?

预期输出:

<root> 
  <somethingelse>
      some data
  <\somethingelse>
  <tag1>
    <tag2>
       <items> // Place where I need to write my items (which is processed)
              <item>
              <\item>
              <item>
              <\item>
       <\items>
    <\tag2>
  <\tag1>
</root>

处理器

@Bean
@StepScope
public ItemProcessor<Item, Item> MyProcessor() {
    return new MyProcessor();
}

作家

@Bean
    @StepScope
    public StaxEventItemWriter<Item> itemXMLWriter() {
        StaxEventItemWriter<Item> xmlFileWriter = new StaxEventItemWriter<>();
        String exportFilePath = "export.xml";
        xmlFileWriter.setResource(new FileSystemResource(new File(exportFilePath)));

    xmlFileWriter.setRootTagName("items");

    Jaxb2Marshaller itemMarshaller = new Jaxb2Marshaller();
    itemMarshaller .setClassesToBeBound(Item.class);
    xmlFileWriter.setMarshaller(itemMarshaller ); 
    return xmlFileWriter;
}

实际输出: 项目与其他元素平行写入,而不是放在items标签中

<root> 
  <somethingelse>
      some data
  <\somethingelse>
  <tag1>
    <tag2>
    <\tag2>
  <\tag1>
</root>
 <items> 
        <item>
        <\item>
        <item>
        <\item>
 <\items>

0 个答案:

没有答案