1

我正在使用 Spring Batch 3.0.8.RELEASE。我想读取 CSV 文件的内容并将其转换为 XML。我熟悉在 Spring Batch 中读取 CSV 文件,但我看到的行为是面向“块”的处理......一次一行,我不确定这种默认行为是否适合我。

这是 CSV 示例:

,WT4RT,AIG-00,694304F,9/1/2017,9/30/2017,"6,975.00",AIG-00201709,10/10/2017,USD,MC
,WT4RT,AIG-00,694317E,9/1/2017,9/30/2017,"2,583.80",AIG-00201709,10/10/2017,USD,MC
,WT4RT,AIG-00,694304G,9/1/2017,9/30/2017,"17,600.00",AIG-00201709,10/10/2017,USD,MC
,WT4RT,AIG-00,694304G,9/1/2017,9/30/2017,740,AIG-00201709,10/10/2017,USD,MC

我需要将此数据转换为以下 XML 格式:

<?xml version="1.0" encoding="UTF-8"?>
<BillingAndCosting version="1.0">
    <ControlArea>
        <SenderId>CMS-BILLING-100</SenderId>
        <WaterMark>92030293829329392030232</WaterMark>
        <RecordCount>2</RecordCount>
        <TimeStamp>2001-12-17T09:30:47-05:00</TimeStamp>
        <DataFileName>String</DataFileName>
    </ControlArea>
    <DataArea>
        <CustomerAccount>
            <ExternalKey>1001</ExternalKey>
            <ExternalSource>HBVenture</ExternalSource>
            <BillingData>
                <ReferenceID>0</ReferenceID>
                <BillingInvoiceNumber>2000016</BillingInvoiceNumber>
                <BillingInvoiceDate>2017-01-31T06:42:07.000Z</BillingInvoiceDate>
                <BillingPeriodFromDate>2017-01-01T06:42:07.000Z</BillingPeriodFromDate>
                <BillingPeriodThruDate>2017-01-31T06:42:07.000Z</BillingPeriodThruDate>
                <BillingInvoiceType>NEW</BillingInvoiceType>
                <BillingAmount CurrencyID="USD">1290.39</BillingAmount>
                <InvoiceItem>
                    <CategoryCode>res-group</CategoryCode>
                    <TaxCategoryID>C2</TaxCategoryID>
                    <InvoiceItemAmount CurrencyID="USD">1290.39</InvoiceItemAmount>
                    <ProductID>694601F</ProductID>
                    <ISVUID>1</ISVUID>
                </InvoiceItem>
            </BillingData>
            <BillingData>
                <ReferenceID>0</ReferenceID>
                <BillingInvoiceNumber>2000017</BillingInvoiceNumber>
                <BillingInvoiceDate>2017-01-31T06:42:07.000Z</BillingInvoiceDate>
                <BillingPeriodFromDate>2017-01-01T06:42:07.000Z</BillingPeriodFromDate>
                <BillingPeriodThruDate>2017-01-31T06:42:07.000Z</BillingPeriodThruDate>
                <BillingInvoiceType>NEW</BillingInvoiceType>
                <BillingAmount CurrencyID="USD">590.39</BillingAmount>
                <InvoiceItem>
                    <CategoryCode>gateway_resource_group</CategoryCode>
                    <TaxCategoryID>C2</TaxCategoryID>
                    <InvoiceItemAmount CurrencyID="USD">590.39</InvoiceItemAmount>
                    <ProductID>694601F</ProductID>
                    <ISVUID>1</ISVUID>
                </InvoiceItem>
            </BillingData>

为简洁起见,我只展示了 XML 的一部分。我不知道的是:如何使用 Spring Batch 读取整个 CSV 文件以填充一个对象,然后我可以将其发送到 XML Marshaller 以转换为 XML。

可以用 Spring Batch 完成,还是我必须“自己动手”?

4

2 回答 2

0

您可以使用FlatFileItemReader读取 CSV 文件。请遵循以下示例代码:

ItemReader 从输入的 csv 文件中逐行读取完整的行。FlatFileItemReader 用于读取 csv 文件。

        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">

            <property name="fieldSetMapper">
                <!-- Mapper which maps each individual items in a record to properties in POJO -->
                <bean class="com.websystique.springbatch.ExamResultFieldSetMapper" />
            </property>

            <property name="lineTokenizer">
                <!-- A tokenizer class to be used when items in input record are separated by specific characters -->
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="delimiter" value="|" />
                </bean>
            </property>

        </bean>

    </property>

</bean>

XML ItemWriter 以 XML 格式写入数据。

<bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
    <property name="resource" value="file:xml/examResult.xml" />
    <property name="rootTagName" value="UniversityExamResultList" />
    <property name="marshaller">
        <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="classesToBeBound">
                <list>
                    <value>com.websystique.springbatch.model.ExamResult</value>
                </list>
            </property>
        </bean>
    </property>
</bean>

您还应该阅读官方 Spring 文档Spring Docs

于 2017-11-29T09:38:57.323 回答
0

我们StaxEventItemWriter用作 ItemWriter(下面的示例)

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

   <bean id = "reportMarshaller" 
      class = "org.springframework.oxm.jaxb.Jaxb2Marshaller">
      <property name = "classesToBeBound"> 
         <list> 
            <value>BillingAndCosting</value> 
         </list> 
      </property> 
   </bean>
于 2017-11-29T06:22:51.497 回答