Dexter
Dexter

Reputation: 4275

Unmarshalling using JAXB gives null in below code?

Can't figure out why the umarshalled object is null in below code. Could you please point out what am I missing?

One more question:

Is it required to run a for loop in the setStaffList(List<Staff> staffList) in Staff class. I am bit confused on this point.

xml:

<?xml version="1.0"?>
<company>
    <staff id="1001">
        <firstname>YW</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff id="2001">
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>
</company>

Unmarshalling code:

public class XmlToList {
    public static void main(String[] args) {
        File xmlFile = new File("D:/CoreJavaPractice/XMLCode/src/staff.xml");
        JAXBContext jaxbContext;
        Company comp;
        try {
            jaxbContext = JAXBContext.newInstance(Company.class);
            comp = (Company) jaxbContext.createUnmarshaller().unmarshal(xmlFile);
            System.out.println(comp + "; " + xmlFile);

            List<Staff> staffList = comp.getStaffList();
            System.out.println(staffList);

            for (Staff s : staffList) {
                System.out.println(s.getFirstname());
            }
        } catch (JAXBException e) {
            e.printStackTrace();
        }    
    }

}

Company Class:

@XmlRootElement(name = "company")
public class Company {
    List<Staff> staffList;

    public Company() {
    }

    public Company(List<Staff> staffList) {
        this.staffList = staffList;

    }

    public List<Staff> getStaffList() {
        return staffList;
    }

    public void setStaffList(List<Staff> staffList) {
        /*
         * for (Staff s : staffList) { this.staffList.add(s); }
         */
        this.staffList = staffList;
    }
}

Staff Class:

@XmlRootElement(name = "staff")
public class Staff {

    Integer id;
    String firstname;
    String lastname;
    String nickname;
    String salary;

    public Staff() {
    }

    public Staff(Integer id, String firstname, String lastname, String nickname, String salary) {
        super();
        this.id = id;
        this.firstname = firstname;
        this.lastname = lastname;
        this.nickname = nickname;
        this.salary = salary;
    }

    public Integer getId() {
        return id;
    }

    @XmlAttribute
    public void setId(Integer id) {
        this.id = id;
    }

    public String getFirstname() {
        return firstname;
    }

    @XmlElement
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    @XmlElement
    public void setLasttname(String lasttname) {
        this.lastname = lasttname;
    }

    public String getNickname() {
        return nickname;
    }

    @XmlElement
    public void setNickname(String nickName) {
        this.nickname = nickName;
    }

    public String getSalary() {
        return salary;
    }

    @XmlElement
    public void setSalary(String salary) {
        this.salary = salary;
    }

}

Upvotes: 1

Views: 854

Answers (3)

Saran
Saran

Reputation: 146

@XmlAccessorType(XmlAccessType.FIELD) on class and @XmlElement(name="staff") on the variable is needed

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "company")
@XmlAccessorType(XmlAccessType.FIELD)
public class Company {

    @XmlElement(name="staff")
    List<Staff> staffList;

    public Company() {
    }

    public Company(List<Staff> staffList) {
        this.staffList = staffList;

    }

    public List<Staff> getStaffList() {
        return staffList;
    }

    public void setStaffList(List<Staff> staffList) {`enter code here`
        /*
         * for (Staff s : staffList) { this.staffList.add(s); }
         */
        this.staffList = staffList;
    }
}

Upvotes: 1

Optional
Optional

Reputation: 4507

Tag List<Staff> staffList with @XmlElement("staff") and you are set

Upvotes: 0

yanefedor
yanefedor

Reputation: 2262

Suggest you to explicitly set XmlAccessorType and XmlElement, by this way your Company class will be:

@XmlRootElement(name = "company")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Company {
    private List<Staff> staffList;

    public Company() {
    }

    public Company(List<Staff> staffList) {
        this.staffList = staffList;

    }

    public List<Staff> getStaffList() {
        return staffList;
    }

    @XmlElement(name = "staff")
    public void setStaffList(List<Staff> staffList) {
        this.staffList = staffList;
    }
}

Upvotes: 1

Related Questions