代码之家  ›  专栏  ›  技术社区  ›  Dexter Arijit Banerjee

在下面的代码中,使用JAXB进行解组会得到null?

  •  1
  • Dexter Arijit Banerjee  · 技术社区  · 8 年前

    无法找出以下代码中umarshalled对象为null的原因。你能指出我遗漏了什么吗?

    还有一个问题:

    是否需要在中运行for循环 setStaffList(List<Staff> staffList) 在员工课堂上。在这一点上我有点困惑。

    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 回复  |  直到 8 年前
        1
  •  1
  •   yanefedor    8 年前

    建议您显式设置 XmlAccessorType XmlElement 通过这种方式,您的公司课程将是:

    @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;
        }
    }
    
        2
  •  1
  •   Saran    8 年前

    @类和上的XmlAccessorType(XmlAccessType.FIELD) @需要变量上的XmlElement(name=“staff”)

    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;
        }
    }
    
        3
  •  0
  •   Optional    8 年前

    标签 List<Staff> staffList 具有 @XmlElement("staff") 你已经准备好了