使用JAXB的解组在下面的代码中给出null?

时间:2017-11-18 09:31:59

标签: java collections jaxb unmarshalling

无法在下面的代码中找出umarshalled对象为空的原因。你能指出我错过了什么吗?

还有一个问题:

是否需要在Staff类的setStaffList(List<Staff> staffList)中运行for循环。我对这一点感到有点困惑。

的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>

解组代码:

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();
        }    
    }

}

公司类:

@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;
    }
}

员工班级:

@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;
    }

}

3 个答案:

答案 0 :(得分:1)

建议您明确设置XmlAccessorTypeXmlElement,这样您的公司类就会:

@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;
    }
}

答案 1 :(得分:1)

类和的

@XmlAccessorType(XmlAccessType.FIELD) 需要@XmlElement(name =&#34; staff&#34;)

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;
    }
}

答案 2 :(得分:0)

使用List<Staff> staffList标记@XmlElement("staff")并设置