【问题标题】:Read from multiple XMLs and write in one XML in Spring batch从多个 XML 读取并在 Spring 批处理中写入一个 XML
【发布时间】:2014-10-14 11:43:07
【问题描述】:

我使用 JAXB xjc 工具从我的多个 xsd 文件生成 java 类(我使用在线工具从我的 xml 文件生成 xsd 文件)。

我的问题是我不知道如何配置我的 context.xml 以使其读取给定的所有类(和 xml)并只生成一个最终的大 xml 文件。

这是我的 context.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
    http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<import resource="../config/context.xml" />
<batch:job id="bghJob" parent="simpleJob">
    <batch:step id="step1">
        <batch:tasklet>
            <batch:chunk reader="multiResourceReader" writer="xmlItemWriter"
                commit-interval="1" />
        </batch:tasklet>
    </batch:step>
</batch:job>

<bean id="multiResourceReader"
    class=" org.springframework.batch.item.file.MultiResourceItemReader">
    <property name="resources" value="classpath:xml/*.xml" />
    <property name="delegate" ref="xmlItemReader" />
</bean>

<bean id="xmlItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
    <property name="unmarshaller" ref="invoiceUnMarshaller" />
    <property name="fragmentRootElementName" value="DocumentType" />
</bean>

<bean id="invoiceUnMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <value>com.xxx.generatedByJaxb.inv.DocumentType</value>
    </property>
</bean>

<bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
    <property name="resource" value="file:xml/outputs/Facture.xml" />
    <property name="marshaller" ref="invoiceMarshaller" />
    <property name="rootTagName" value="Facture" />
</bean>

<bean id="invoiceMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <value>com.xxx.generatedByJaxb.inv.DocumentType</value>
    </property>
</bean>

似乎我只能读取一个类(例如 com.xxx.generatedByJaxb.in​​v.DocumentType),我必须指定根标签,但 我生成的 java 中没有一个类有注释 XmlRootElement

请问如何配置我的工作以实现我的目标?

谢谢。

【问题讨论】:

  • 我也有同样的要求...读取多个xml文件并用一些额外的数据写入xml文件。你能告诉我怎么做吗?你能和我分享一下这个例子吗?

标签: xml jaxb spring-batch jaxb2 spring-oxm


【解决方案1】:

只需使用contextPath 属性:

<property name="contextPath"
   value="com.xxx.generatedByJaxb.inv:com.yyy.generatedByJaxb.inv"/>

这是相关软件包的列表,:-separated。

还要检查其他属性,它们可能会很方便。

【讨论】:

  • 你有多少课程并不重要。你只需要指定可能不多的包。
  • 我应该使用这个属性而不是 classesToBeBound 吗?
  • 它可以工作(我认为),但指定“两个类具有相同的 XML 类型名称“XXX”时发生错误。使用 XmlType.name 和 XmlType.namespace 为它们分配不同的名称。 "
  • 请再问一个问题,在那里发布您的配置等。
  • 这是一个不同的问题,请分开问。
猜你喜欢
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
  • 2014-02-13
  • 2020-08-16
  • 2020-06-15
相关资源
最近更新 更多