简体   繁体   中英

How to write a mapping.xml file when we are trying to read Multiple input Formats using BeanIO

My input file has multiple formats. My first line will be delimited and the remaining file will be fixed length.

How to write my mapping xml file and read it from java?

I tried writing multiple streams but that didn't work.

My mapping.xml is something like this,

<beanio xmlns="http://www.beanio.org/2012/03"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">
    <stream name="ebcdicFile" format="delimited">
        <parser>
            <property name="delimiter" value="|" />
        </parser>
        <record name="header"
            class="sft_action_cms_apt_394.sft_action_cms_apt_394.FileSegment">
            <field name="fileName" />
            <field name="batchCount" />
            <field name="totalRecords" />
            <field name="maxBatchSize" />
            <field name="pickUpTime" />
            <field name="errorFlag" />
        </record>
    </stream>
    <stream name="file" format="fixedlength">
        <record name="dec"
            class="sft_action_cms_apt_394.sft_action_cms_apt_394.pojo">
            <field name="tag" length="4" />
            <field name="description" length="unbounded" />
        </record>
    </stream>
</beanio>

I don't think your use case is covered by BeanIO. I would try to do something like this:

  • Make sure you use some Reader implementation that supports marking & reset. An example being the standard java.io.BufferedReader
  • First mark() your current position on the input of the BufferedReader .
  • Read as much of the data you need to manually identify which one of your 2 BeanIO streams to use for reading the data and converting it for you into your objects.
  • Now reset() the BufferedReader , this will make it go "back" to the same point as where you have called mark() .
  • Now use BeanIO to read your data.

You may need to repeat the above process or tweak it depending on the structure of your data. You don't show if your "header" is a single record or if it can occur multiple times in between "dec" records.

If you only have a single "header" record and then multiple "dec" records you could simplify the process above with something like:

  • Read the first line of your data directly with the BufferedReader.readLine() method into a String . Pass this string to the BeanIO reader to parse it and give you the "header" object back.

     String firstLine = bufferedReader.readLine(); BeanReader beanReader = null; FileSegment fileSegment = null; try (Reader in = new BufferedReader(new StringReader(firstLine))) { beanReader = factory.createReader("ebcdicFile", in); fileSegment = (FileSegment)beanReader.read(); } 
  • Now you can create another BeanReader to read the rest of your input data using the "file" stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM