JAXB 解析 xml 文件,其中属性位于一行

JAXB parsing an xml file where properties are on a single line

我有这个特定的 xml 文件(采用完全相同的格式),我正在尝试使用 JAXB 进行解析 因为属性都在一行上,所以它看不到它们,并且 returns 在我的主要函数中所有字段都为空。如何正确解析 xml 的格式?

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

这是员工代码

@XmlRootElement(name = "employee")
@XmlAccessorType (XmlAccessType.FIELD)
public class Employee implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    private Integer id;
    private String firstName;
    private String lastName;
 
    public Employee() {
        super();
    }
 
    //Setters and Getters
 
    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
    }
   }

员工代码:

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

}

这是主要执行:

public static void main(String[] args) 
        {
            String fileName = "employee.xml";
     
            jaxbXmlFileToObject(fileName);
        }
     
        private static void jaxbXmlFileToObject(String fileName) {
             
            File xmlFile = new File(fileName);
             
            JAXBContext jaxbContext;
            try
            {
                jaxbContext = JAXBContext.newInstance(Employees.class);
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                
                Employees employees = (Employees) jaxbUnmarshaller.unmarshal(xmlFile);
                 
                for(Employee e: employees.getEmployees() )
                System.out.println(e);
            }
            catch (JAXBException e) 
            {
                e.printStackTrace();
            }
        }

尝试:

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 + "]";
    }
   }

Employees.java:

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

}

输出:

注:

  1. Main没有变化。
  2. 已更新 EmployeeEmployees class。
  3. Employee 中添加了 @XmlAttribute 以映射属性名称和 getter/setters。
  4. Employees 中添加了 @XmlElement 以映射 employees 标记和构造函数中的每个 employee 元素。