0

The requirement is to skip field when its empty. eg -

    <segment name="seg1" class="com.company.bean.segmentBean" xmlType="none">   
            <field name="field1" xmlName= "fieldXml1" xmlType="attribute" maxLength="7" />
            <field name="field2" xmlName= "fieldX2l1" xmlType="attribute" maxLength="1" typeHandler="Handler" />
    </segment>

Lets assume that field2="". As the value of field2 is "". I would like to have the field skipped in segment. Basically the end result XML shouldnt display field2 as its empty("").

1 Answers1

0

You need the lazy attribute on field2. As stated in the Reference Guide

lazy - Set to true to convert empty field text to null before type conversion. For repeating fields bound to a collection, the collection will not be created if all field values are null or the empty String. Defaults to false.

This will make field2 = null and by default, most XML libraries will not output any null elements, BeanIO included.

Try this for field2:

<field name="field2" lazy="true" xmlName= "fieldX2l1" xmlType="attribute" maxLength="1" typeHandler="Handler" />

Most of the time, I also combine lazy with trim. From the docs:

trim - Set to true to trim the field text before validation and type conversion. Defaults to false.

<field name="field2" lazy="true" trim="true" xmlName= "fieldX2l1" xmlType="attribute" maxLength="1" typeHandler="Handler" />
nicoschl
  • 2,346
  • 1
  • 17
  • 17