xmlspring-batchfilesizexmlwriter

In Spring batch xml writing can we write complex xml for specific nos of records


I have to design a spring batch job which reads from database (suppose JdbcCursorItemReader) and write the data in to XML (complex xml as shown below). I have following queries. a). XML should not exceed more than 8k records. If there are more records, a new XML to be written. For e.g. There are 10K records fetched from the database. REQ_1.xml -> 8k records REQ_2.xml -> 2k records

b). How can I dynamically attach the Header block (header & summary) while writing the XML for both xml.

Thanks in advance.

The xml structure is as below

<?xml version="1.0" encoding="UTF-8"?>
<faxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ST_BD0.xsd">
    <header>
        <extsysname>CU</extsysname>
        <datpost>2013-04-20</datpost>
    </header>
    <summary>
        <countdr>4000</countdr>
        <countcr>4000</countcr>
        <sumdr>1600000.00</sumdr>
        <sumcr>1600000.00</sumcr>
    </summary>
    <txnlist>
        <txndata>
            <debit>
                <accountno>6000000000029</accountno>
                <amount>400.00</amount>
            </debit>
            <credit>
                <accountno>022222222222222</accountno>
                <amount>400.00</amount>
            </credit>
        </txndata>
        <txndata>
            <debit>
                <accountno>6000000000029</accountno>
                <amount>400.00</amount>
            </debit>
            <credit>
                <accountno>022222222222222</accountno>
                <amount>400.00</amount>
            </credit>
        </txndata>
    </txnlist>
</faxml>

Solution

  • You can use the MultiResourceItemWriter for that and set the itemCountLimitPerResource to 8000. You can also customize the suffix to be _1, _2, etc using a custom ResourceSuffixCreator.