如何使用Spring批处理将CSV文件的内容转换为XML?

时间:2017-11-28 20:31:45

标签: spring csv spring-batch oxm

我使用的是Spring Batch 3.0.8.RELEASE。我想阅读CSV文件的内容并将其转换为XML。我熟悉在Spring Batch中读取CSV文件,但我所看到的行为是" chunk"面向处理....一次一行,我不确定这个默认行为对我有用。

以下是CSV示例:

<?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格式:

ember install ember-bootstrap

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

是否可以使用Spring批次完成,或者我是否必须自己推出&#39;?

2 个答案:

答案 0 :(得分:0)

我们使用npm config delete proxy作为ItemWriter(下面的示例)

StaxEventItemWriter

答案 1 :(得分: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