user2568887
user2568887

Reputation: 35

JAXB unmarshalling issue for an object containing collection of other objects

I am trying to unmarshall the following XML document into JAXB java objects.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <company>
    <name>ABC Inc</name>
    <address>123 1st Street, My City, AB</address>
    <employees>
    <employee>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
    </employee>
    <employee>
    <firstName>John2</firstName>
    <lastName>Doe2</lastName>
    </employee>
    </employees>
    </company>

Using the following java code, but the child collection (company.getEmployees()) did not contain any elements even though the XML contained two elements.

    JAXBContext jc = JAXBContext.newInstance("com.abc");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    JAXBElement<Company> root = unmarshaller.unmarshal(new StreamSource(new FileInputStream( "TestCompany.xml" )), Company.class);
    Company company = root.getValue();
    List<Employee> employees = company.getEmployees();

Here are the JAXB generated classes:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "company", propOrder = {
        "name",
        "address",
        "employees"
    })
    public class Company {
        @XmlElement(required = true)
        protected String name;
        @XmlElement(required = true, nillable = true)
        protected String address;
        protected List<Employee> employees;

        public String getName() {
            return name;
        }
        public void setName(String value) {
            this.name = value;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String value) {
            this.address = value;
        }
        public List<Employee> getEmployees() {
            if (employees == null) {
                employees = new ArrayList<Employee>();
            }
            return this.employees;
        }
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "employee", propOrder = {
        "firstName",
        "lastName"
    })
    public class Employee {

        @XmlElement(required = true)
        protected String firstName;
        @XmlElement(required = true)
        protected String lastName;

        public String getFirstName() {
            return firstName;
        }
        public void setFirstName(String value) {
            this.firstName = value;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String value) {
            this.lastName = value;
        }
    }

Upvotes: 2

Views: 1186

Answers (1)

bdoughan
bdoughan

Reputation: 149047

By default JAXB inplementations will not add a grouping element for collections. You need to do the following and leverage @XmlElementWrapper:

@XmlElementWrapper
@XmlElement(name="employee")
protected List<Employee> employees;

For More Information

Upvotes: 3

Related Questions