2

I'm trying to generate the following xml file which has 2 fields as Header and the Repeating section "rec" node :

<?xml version="1.0" encoding="UTF-8"?>

<transaction>

<createDate>20160708</createDate>
<dlrCode>100<dlrCode/>

<rec>
    <processDate>20190108</processDate>
    <srcID/>10<srcID/>
</rec>
<rec>
    <processDate>20190108</processDate>
    <srcID/>11<srcID/>
</rec>
<rec>
    <processDate>20190108</processDate>
    <srcID/>12<srcID/>
</rec>

</transaction>

This is the mapping file which I created:

<?xml version="1.0" encoding="UTF-8"?>
<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="dist" format="xml" xmlName="transaction" >
     <record name="HeaderRecord" class="com.myPackage.HeaderRecord" minOccurs="1" maxOccurs="1" order="1" > 
        <field name="createDate" format="yyyyMMdd" />
        <field name="dlrCode" />
    </record>
    <record name="DisRecord" class="com.myPackage.Record" minOccurs="0" maxOccurs="unbounded" xmlName="rec" order="2">
        <field name="processDate" format="yyyyMMdd"/>
        <field name="srcID"/> 
    </record>
 </stream>
</beanio>

But the problem is, it generates the header fields inside the HeaderRecord node like this:

<?xml version="1.0" encoding="UTF-8"?>

<transaction>

<HeaderRecord>
     <createDate>20160708</createDate>
     <dlrCode>100<dlrCode/>
</HeaderRecord>

<rec>
    <processDate>20190108</processDate>
    <srcID/>10<srcID/>
</rec>
<rec>
    <processDate>20190108</processDate>
    <srcID/>11<srcID/>
</rec>
<rec>
    <processDate>20190108</processDate>
    <srcID/>12<srcID/>
</rec>
</transaction>

Is there something misconfigured in the mapping file? How to achieve the desired output?

Ben
  • 21
  • 3
  • I think you should show us how you use the `BeanWriter` together with the above mapping file to achieve your first (desired) output. – nicoschl Jan 10 '19 at 21:18
  • The xml data is huge, I do multiple calls to BeanWriter.write(..) per each repeating POJO (detail) and one call for header POJOto avoid any out of memory (heap space) issue. – Ben Jan 11 '19 at 17:56

1 Answers1

1

By using the xmlType="none" attribute you can control if an xml element should be produced or not. The xmlName is by default equal to the record name when you don't specify a xmlName attribute, see here. A record will always be mapped to an xml element and with the use of segments, you might be able to get the desired output.

Try this mapping file:

<stream name="dist" format="xml" xmlType="none" >
  <record name="HeaderRecord" class="com.mypackage.HeaderRecord" minOccurs="1" maxOccurs="1" xmlName="transaction">
    <segment name="dummy" xmlType="none">
      <field name="createDate" format="yyyyMMdd" />
      <field name="dlrCode" />
    </segment>
  </record>

I don't think it is 100% what you are looking for though.

nicoschl
  • 2,346
  • 1
  • 17
  • 17
  • It doesn't generate the layout that I'm looking for ,The must be end of the file, but by applying this config it creates the header fields inside the , and multiple outside the – Ben Jan 11 '19 at 18:27
  • I don't see any way of achieving wat you ask for only with `BeanIO`, without changing your data structure. I get your concern with the heap space etc. Does it have to be `BeanIO`? – nicoschl Jan 11 '19 at 22:51