0

Please help me to remove unwanted extra lines for below part.

Current Output:

001  Name1Str1 CA   1234 
001  Name2Str2 IN   4321 
005  30Manager Technology
005  50Lead Technology

Java Code:

        ArrayList<Employees1> emps = new ArrayList<>();

        ArrayList<Employee1> empp1 = new ArrayList<>();
        ArrayList<Employee2> empp2 = new ArrayList<>();

        Employees1 employees = new Employees1();

        Employee1 emp1 = new Employee1();
        emp1.setSno("001");
        emp1.setName("Name1");
        emp1.setStreet("Str1");
        emp1.setCity("CA");         
        emp1.setZip("1234");
        empp1.add(emp1);

        Employee2 emp2 = new Employee2();
        emp2.setSno("005");
        emp2.setAge("30");          
        emp2.setPosition("Manager Technology");
        empp2.add(emp2);

        Employee1 emp12 = new Employee1();
        emp12.setSno("001");
        emp12.setName("Name2");
        emp12.setStreet("Str2");
        emp12.setCity("IN");            
        emp12.setZip("4321");
        empp1.add(emp12);

        Employee2 emp22 = new Employee2();
        emp22.setSno("005");
        emp22.setAge("50");         
        emp22.setPosition("Lead Technology");
        empp2.add(emp22);

        employees.setEmployee1(empp1);
        employees.setEmployee2(empp2);

        emps.add(employees);

mapping.xml

<stream name="employeeStream" format="fixedlength">
        <group name="employees" class="com.Employees1">
            <record name="employee1" class="com.Employee1" minOccurs="0"
                maxOccurs="unbounded" collection="list">
                <field name="sno" length="5" rid="true" literal="001" />
                <field name="name" length="5" />
                <field name="street" length="5" />
                <field name="city" length="5" />
                <field name="zip" length="5" />
            </record>
            <record name="employee2" class="com.Employee2" minOccurs="0"
                maxOccurs="unbounded" collection="list">
                <field name="sno" length="5" rid="true" literal="005" />
                <field name="age" length="2" />
                <field name="position" length="18" />
            </record>
        </group>
    </stream>

JavaCode:

public class Employee1 {    
    private String sno;
    private String name;
    private String street;
    private String city;
    private String zip;
}

public class Employee2 {    
    private String sno;
    private String age;
    private String position;
}

public class Employees1 implements Serializable 
{
private List<Employee1> employee1 = null;
private List<Employee2> employee2 = null;
}
sunleo
  • 10,589
  • 35
  • 116
  • 196
  • Still had no coffee, but `minOccurs="0"` perhaps? – Joop Eggen Jan 17 '18 at 07:33
  • Please show us the `Employees1` class and the code where you write the output? Which object(s) are you passing to the `BeanWriter.write()` method? – nicoschl Jan 17 '18 at 07:54
  • 1
    The config does what you asked it to do. You've asked it to output the same set of employees twice. One with sno "001" and fields name,street,city,zip and once again with sno "005" and only fields "age" and "position". If you don't want duplicate outputs, you should have two lists - one with the employees that you want to output in format 001 and one list that you want in format 002. And they should probably have two different Java classes as well, what's the point of having one Employee1 class if you use it for different purposes with no overlapping fields? – Erwin Bolwidt Jan 17 '18 at 07:57
  • @ErwinBolwidt Keeping two lists is working but order of the objects is not maintained.Is there a way to maintain order of the objects too? Thanks for your response. – sunleo Jan 17 '18 at 08:49
  • I'm not very familiar with this framework, I'm just following your XML logically. But I don't see how you could keep it in order as there is no way for the framework to find out whether you want to output in format 001 or 003. Records have the same Employee1 class and there is nothing that the framework can use to decide the choice of format. Maybe if you had a field "format", and if the framework has support for conditionals, you could make it work that way. – Erwin Bolwidt Jan 17 '18 at 08:56

0 Answers0