首页 > 解决方案 > 解析 XML 时获取 null

问题描述

我正在尝试使用 JaxB API 解析字符串 xml。

下面是我的代码

XML

http://www.devx.com/supportitems/showSupportItem.php?co=38898&supportitem=listing1

Pojo 课程

@XmlRootElement(name = "employees")
public class FileWrapper {
    private LinkedHashSet<Employee> employees;

    public LinkedHashSet<Employee> getEmployees() {
        return employees;
    }

    @XmlElement(name = "employee")
    public void setEmployees(LinkedHashSet<Employee> employees) {
        this.employees = employees;
    }

}

雇员.java

import javax.xml.bind.annotation.XmlAttribute;

public class Employee {
    private String division;

    private String firstname;

    private String id;

    private String title;

    private String building;

    private String room;

    private String supervisor;

    private String lastname;

    public String getDivision() {
        return division;
    }

    @XmlAttribute
    public void setDivision(String division) {
        this.division = division;
    }

    public String getFirstname() {
        return firstname;
    }

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

    public String getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    @XmlAttribute
    public void setTitle(String title) {
        this.title = title;
    }

    public String getBuilding() {
        return building;
    }

    @XmlAttribute
    public void setBuilding(String building) {
        this.building = building;
    }

    public String getRoom() {
        return room;
    }

    @XmlAttribute
    public void setRoom(String room) {
        this.room = room;
    }

    public String getSupervisor() {
        return supervisor;
    }

    @XmlAttribute
    public void setSupervisor(String supervisor) {
        this.supervisor = supervisor;
    }

    public String getLastname() {
        return lastname;
    }

    @XmlAttribute
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    @Override
    public String toString() {
        return "ClassPojo [division = " + division + ", firstname = " + firstname + ", id = " + id + ", title = "
                + title + ", building = " + building + ", room = " + room + ", supervisor = " + supervisor
                + ", lastname = " + lastname + "]";
    }
}

主条目

public class TestEntryPoint {

    public static void main(String[] args) throws JAXBException {

        JAXBContext jaxbContext = JAXBContext.newInstance(FileWrapper.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        StringReader reader = new StringReader("XML from above link as String");
        FileWrapper person = (FileWrapper) unmarshaller.unmarshal(reader);

        List<Employee> emp = new ArrayList<Employee>(person.getEmployees());

        System.out.println(emp.get(0).getFirstname());
    }

}

所以当我试图提取任何标签的值时,它显示为空。我的 XML 的数据结构或 pojo 类有什么问题吗?从过去的几个小时开始,我就陷入了困境。

做错了什么?请问Canyone建议?

谢谢

标签: javajaxb

解决方案


Employee 类中的注解引用 XML 属性而不是元素。

这里是修正版:

public class Employee {

    private String division;

    private String firstname;

    private String id;

    private String title;

    private String building;

    private String room;

    private String supervisor;

    private String lastname;

    public String getDivision() {
        return division;
    }

    @XmlElement(name = "division")
    public void setDivision(String division) {
        this.division = division;
    }

    public String getFirstname() {
        return firstname;
    }

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

    public String getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    @XmlElement(name = "title")
    public void setTitle(String title) {
        this.title = title;
    }

    public String getBuilding() {
        return building;
    }

    @XmlElement(name = "building")
    public void setBuilding(String building) {
        this.building = building;
    }

    public String getRoom() {
        return room;
    }

    @XmlElement(name = "room")
    public void setRoom(String room) {
        this.room = room;
    }

    public String getSupervisor() {
        return supervisor;
    }

    @XmlElement(name = "supervisor")
    public void setSupervisor(String supervisor) {
        this.supervisor = supervisor;
    }

    public String getLastname() {
        return lastname;
    }

    @XmlElement(name = "lastname")
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    @Override
    public String toString() {
        return "ClassPojo [division = " + division + ", firstname = " + firstname + ", id = " + id + ", title = "
                + title + ", building = " + building + ", room = " + room + ", supervisor = " + supervisor
                + ", lastname = " + lastname + "]";
    }
}

推荐阅读