Here is my mapping.xml
<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="employeeFile" format="fixedlength">
<record name="employee" class="poc.bean.io.Employee">
<field name="firstName" length="10"/>
<field name="lastName" length="10"/>
<field name="title" length="20"/>
<field name="salary" length="10"/>
<field name="hireDate" format="MMddyyyy" length="8"/>
<segment name="addressList" collection="list" class="poc.bean.io.Address" minOccurs="1" maxOccurs="unbounded">
<field name="street" length="40"/>
<field name="city" length="20"/>
<field name="state" length="2"/>
<field name="zip" length="5"/>
<segment name="phoneList" collection="list" class="poc.bean.io.Phone" minOccurs="1" maxOccurs="unbounded">
<field name="phone" length="10"/>
</segment>
</segment>
</record>
</stream>
</beanio>
I want to use beanio to write out my Employee into a fixed length string. My writer class is
package
poc.bean.io;
import org.beanio.*;
import java.io.*;
import java.util.*;
public class BeanWriterExample {
public static void main(String[] args) throws Exception {
// create a StreamFactory
StreamFactory factory = StreamFactory.newInstance();
// load the mapping file
factory.load("test/mapping/mapping.xml");
Employee employee = new Employee();
employee.setFirstName("Jennifer");
employee.setLastName("Jones");
employee.setTitle("Marketing");
employee.setSalary(60000);
employee.setHireDate(new Date());
Address address1 = new Address();
address1.setStreet("100 General Hancock Blvd");
address1.setCity("North Wales");
address1.setState("PA");
address1.setZip("19454");
List<Phone> phoneList=new ArrayList<Phone>();
phoneList.add(new Phone("1234567890"));
phoneList.add(new Phone("0000000000"));
address1.setPhoneList(phoneList);
Address address2 = new Address();
address2.setStreet("14101 Atwater Dr");
address2.setCity("Sterling Heights");
address2.setState("MI");
address2.setZip("48318");
List<Phone> phoneList2=new ArrayList<Phone>();
phoneList2.add(new Phone("
8888888888"));
phoneList2.add(new Phone("9999999999"));
address2.setPhoneList(phoneList2);
List<Address> listAddress = new ArrayList<Address>();
listAddress.add(address1);
listAddress.add(address2);
employee.setAddressList(listAddress);
// use a StreamFactory to create a BeanWriter
BeanWriter out = factory.createWriter("employeeFile", new File("output/employee.txt"));
// write an Employee object directly to the BeanWriter
out.write(employee);
out.flush();
out.close();
}
}
However, I got error output as below. How to fix it? Thanks
Exception in thread "main" org.beanio.BeanIOConfigurationException: Invalid segment 'addressList', in record 'employee', in stream 'employeeFile': A segment of indeterminate size may not follow another component of indeterminate size
at org.beanio.internal.compiler.ProcessorSupport.process(ProcessorSupport.java:93)
at org.beanio.internal.compiler.ParserFactorySupport.createStream(ParserFactorySupport.java:83)
at org.beanio.internal.compiler.StreamCompiler.createStreamDefinitions(StreamCompiler.java:130)
at org.beanio.internal.compiler.StreamCompiler.loadMapping(StreamCompiler.java:84)
at org.beanio.internal.DefaultStreamFactory.load(DefaultStreamFactory.java:52)
at org.beanio.StreamFactory.load(StreamFactory.java:258)
at org.beanio.StreamFactory.load(StreamFactory.java:234)
at org.beanio.StreamFactory.load(StreamFactory.java:223)
at poc.bean.io.BeanWriterExample.main(BeanWriterExample.java:12)