0

Below current output is having totally 28 chars

1Tit FNameLName          EOL
2Tit           StrCA12345EOL

Below expected output is having totally 18 chars

1Tit FNameLNameEOL
2Tit StrCA12345EOL

I am trying to set no space if segment object is null so that I can have detail1 or detail2 but not both, please help to do it.Thanks.

mapping.xml

<stream name="employeeStream" format="fixedlength">
        <group name="employees" class="com.Employees"
            minOccurs="0" maxOccurs="unbounded">
            <record name="employee" class="com.tEmployee"
                collection="list" minOccurs="1" maxOccurs="unbounded">
                <field name="title" length="5"/>
                <segment name="detail1" class="com.Details1"
                    minOccurs="0" maxOccurs="1">
                    <field name="firstName" length="5"/>
                    <field name="lastName" length="5"/>
                </segment>
                <segment name="detail2" class="com.Details2"
                    minOccurs="0" maxOccurs="1">
                    <field name="street" length="3" />
                    <field name="city" length="2" />
                    <field name="zip" length="5" />
                </segment>
                <field name="end" length="3"/>
            </record>
        </group>
    </stream>

Java code used

ArrayList<Employees> emps = new ArrayList<>();
ArrayList<Employee> emp = new ArrayList<>();

Employees  employees  = new Employees();

Details1 detail1 = new Details1();
detail1.setFirstName("FName");
detail1.setLastName("LName");                                                   

Employee emp1 = new Employee();
emp1.setTitle("1Tit");
emp1.setDetail1(detail1);
emp1.setEnd("EOL");
emp.add(emp1);

Details2 detail2 = new Details2();
detail2.setStreet("Str");
detail2.setCity("CA");
detail2.setZip("12345");

Employee emp2 = new Employee();
emp2.setTitle("2Tit");
emp2.setEnd("EOL");
emp2.setDetail2(detail2);
emp.add(emp2);

employees.setEmployee(emp);

emps.add(employees);
sunleo
  • 10,589
  • 35
  • 116
  • 196
  • You've specified the file to be of fixed length. If empty fields would just be completely omitted then it wouldn't be a fixed-length file. – Erik Karlstrand Jan 16 '18 at 07:09
  • @noMad17 thanks for your reply, is there a way to achieve above out put? – sunleo Jan 16 '18 at 07:17
  • You could perhaps try to set `nillable` on your segments and see if that works. `` – Erik Karlstrand Jan 16 '18 at 08:43
  • @noMad17 Thanks for response but minOccurs="0" nillable="true" is not working.Is there any other way to accomplish this format? – sunleo Jan 16 '18 at 08:50
  • If you have some weird/custom format then just write some java code to transform the data as you need, maybe as an extra step after beanio - otherwise you can spend days trying to figure this out. Also check out the beanio website and its documentation for more details on using beanio. – Claus Ibsen Jan 17 '18 at 08:13

0 Answers0