Java JAXB解析属性位于单行上的xml文件

Java JAXB解析属性位于单行上的xml文件,java,xml-parsing,jaxb,Java,Xml Parsing,Jaxb,我有一个特定的xml文件(正是这种格式),我正试图用JAXB解析它 因为所有属性都在一行上,所以它看不到它们,并且在我的主函数中,所有字段都返回为null。如何正确解析xml的格式 <?xml version="1.0" encoding="UTF-8"?> <employees> <employee firstName="Asya" id="2" lastname="Olsh

我有一个特定的xml文件(正是这种格式),我正试图用JAXB解析它 因为所有属性都在一行上,所以它看不到它们,并且在我的主函数中,所有字段都返回为null。如何正确解析xml的格式

<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee firstName="Asya" id="2" lastname="Olshansky"/>
</employees>
员工代码:

@XmlRootElement(name = "employees")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employees {
    @XmlElement(name = "employee")
    List<Employee> employees = null;

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

    public void setEmployees(List<Employee> list) {
        this.employees = list;
    }

}
尝试:

Employee.java:

public class Employee implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    private Integer id;
    private String firstName;
    private String lastName;
 
    public Employee() {
        super();
    }
    
    public Employee(Integer id, String firstName, String lastName) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @XmlAttribute(name="id")
    public Integer getId() {
        return id;
    }

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

    @XmlAttribute(name="firstName")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @XmlAttribute(name="lastname")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
    }
   }
@XmlRootElement(name="employees")
public class Employees {
    
    List<Employee> employees;
    
    public Employees() {}
    
    public Employees(List<Employee> employees) {
        super();
        this.employees = employees;
    }

    @XmlElement(name="employee")
    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> list) {
        this.employees = list;
    }

}
Employees.java:

public class Employee implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    private Integer id;
    private String firstName;
    private String lastName;
 
    public Employee() {
        super();
    }
    
    public Employee(Integer id, String firstName, String lastName) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @XmlAttribute(name="id")
    public Integer getId() {
        return id;
    }

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

    @XmlAttribute(name="firstName")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @XmlAttribute(name="lastname")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
    }
   }
@XmlRootElement(name="employees")
public class Employees {
    
    List<Employee> employees;
    
    public Employees() {}
    
    public Employees(List<Employee> employees) {
        super();
        this.employees = employees;
    }

    @XmlElement(name="employee")
    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> list) {
        this.employees = list;
    }

}
@XmlRootElement(name=“employees”)
公营雇员{
列出员工名单;
公职人员(){}
公共雇员(名单雇员){
超级();
这是。雇员=雇员;
}
@xmlement(name=“employee”)
公开名单{
返回员工;
}
公众雇员(名单){
这是一份员工名单;
}
}
输出:

public class Employee implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    private Integer id;
    private String firstName;
    private String lastName;
 
    public Employee() {
        super();
    }
    
    public Employee(Integer id, String firstName, String lastName) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @XmlAttribute(name="id")
    public Integer getId() {
        return id;
    }

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

    @XmlAttribute(name="firstName")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @XmlAttribute(name="lastname")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
    }
   }
@XmlRootElement(name="employees")
public class Employees {
    
    List<Employee> employees;
    
    public Employees() {}
    
    public Employees(List<Employee> employees) {
        super();
        this.employees = employees;
    }

    @XmlElement(name="employee")
    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> list) {
        this.employees = list;
    }

}

注意:

public class Employee implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    private Integer id;
    private String firstName;
    private String lastName;
 
    public Employee() {
        super();
    }
    
    public Employee(Integer id, String firstName, String lastName) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @XmlAttribute(name="id")
    public Integer getId() {
        return id;
    }

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

    @XmlAttribute(name="firstName")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @XmlAttribute(name="lastname")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
    }
   }
@XmlRootElement(name="employees")
public class Employees {
    
    List<Employee> employees;
    
    public Employees() {}
    
    public Employees(List<Employee> employees) {
        super();
        this.employees = employees;
    }

    @XmlElement(name="employee")
    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> list) {
        this.employees = list;
    }

}
  • Main
    中没有更改
  • 更新了
    员工
    员工
    课程
  • Employee
    中添加了
    @xmldattribute
    ,以映射属性名称和getter/setter
  • Employees
    中添加了
    @xmlement
    ,以映射
    Employees
    标记和构造函数中的每个
    Employees
    元素

  • 你能解释一下为什么用@XmlAttribute代替@XmlElement吗?或者你能重定向到一个我可以阅读的来源吗it@Asa
    @xmldattribute
    在您的案例中用于属性,即
    id
    firstname
    等,
    @xmldelement
    在您的案例中用于指示元素名称,即
    employee
    。让我知道如果你成功了是的成功了!我在哪里可以阅读更多关于它的信息以了解何时使用什么?