如何在Spring批处理中使用StaxEventItemReader读取头信息

时间:2016-09-21 11:29:44

标签: xml spring spring-batch

我在Spring批处理中使用StaxEventItemReader来读取XML数据并将其映射到java对象。我能够成功地读取XML中的重复元素,但问题是我想从XML中读取头信息。是否有任何来自StaxEventItemReader的方法或支持来读取标题信息。

 <?xml version="1.0" encoding="UTF-8"?>
<Contracts>
    <TransactionInfo><Receiver>
            <Code>CAT</Code>
            <Name>CAT</Name>
        </Receiver>
        <SourceFile>
            <FilePath>\\nft2a\ft$\transfer\EAD7SRR1.20160510_02363859.xml
            </FilePath>
            <FileName>EAD7SRR1.20160510_02363859.xml
            </FileName>
            <FileType>XML</FileType>
            <FileTime>2016-05-10T02:36:41.337-07:00
            </FileTime>
        </SourceFile>
    </TransactionInfo>
    <Contract ContractID="Contract Rate Feed Test - LCL" 
        Carrier="DHL Danzas Air &amp; Ocean" TermBegin="2015-09-15" TermEnd="2016-09-14"
        CarrierOrgID="20455">
        <LineItem ReferenceID="135516504144" Origin="Antwerp, BE" OriginCity="Antwerp" DestinationCity="Kpeme">
            <Surcharge Amount="25.0" Basis="BILLADING" Code="CUSTOM" Name="Document Turnover Fee" CurrencyCode="USD" />
            <Surcharge Amount="10.0" Basis="KILO" Code="CUSTOM" Name="Hazardous" CurrencyCode="USD" />
        </LineItem>
        <LineItem ReferenceID="135516504145" Origin="Antwerp, BE"OriginCity="Antwerp" DestinationCity="Libreville">
            <Surcharge Amount="8.0" Basis="KILO" Code="CUSTOM" Name="Origin Handling"
                CurrencyCode="USD" />
        </LineItem>
    </Contract>
    <Contract ContractID="Contract_1005_Test_LCL" 
        GTNContractID="66860" CarrierOrgID="20455">
        <LineItem ReferenceID="135516559003" Origin="Hong Kong, HK" DestinationCity="Hamburg">
            <Surcharge Amount="10.0" Basis="KILO" Code="ACD" Name="Arbitraries Charge (Destination)" CurrencyCode="USD" />
        </LineItem>
    </Contract>
</Contracts>

如何阅读TransactionInfo 我的配置是

<bean id="contractReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
 <property name="resource" value="classpath:sample-inputfile-srr-ocean.xml" />
    <property name="fragmentRootElementName" value="Contract" />
    <property name="unmarshaller">
        <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="classesToBeBound">
                <list>
                    <value>com.cat.srr.gtn.domain.Contract</value>
                </list>
            </property>
        </bean>
    </property>
</bean>
<batch:job id="gtnContractJob">

    <batch:step id="step1" next="step2">
        <tasklet ref="ftpPullTask" />
    </batch:step>

    <batch:step id="step2">
        <batch:tasklet transaction-manager="transactionManager">
            <batch:chunk reader="multiResourceReader" writer="contractWriter"
                processor="contractProcessor" commit-interval="10" />
        </batch:tasklet>
    </batch:step>

    <batch:listeners>
        <batch:listener ref="jobListener" />
    </batch:listeners>
</batch:job>

请帮助我如何使用spring batch读取标题信息。

1 个答案:

答案 0 :(得分:0)

使用StaxEventItemReader

的以下属性
  

fragmentRootElementNames

所以你的contractReader看起来应该是这样的

<bean id="contractReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
 <property name="resource" value="classpath:sample-inputfile-srr-ocean.xml" />
    <property name="fragmentRootElementNames" value="TransactionInfo,Contract" />
    <property name="unmarshaller">
        <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="classesToBeBound">
                <list>
                    <value>com.cat.srr.gtn.domain.Contract</value>
<!-- Assumed that TransactionInfo is in same package as Contract -->
                    <value>com.cat.srr.gtn.domain.TransactionInfo</value>
                </list>
            </property>
        </bean>
    </property>
</bean>

现在如何在处理器中处理它取决于你