【问题标题】:JAXB parsing an xml file where properties are on a single lineJAXB 解析属性位于单行的 xml 文件
【发布时间】:2020-07-05 17:44:02
【问题描述】:

我有这个特定的 xml 文件(以那种确切的格式),我试图用 JAXB 解析它 因为属性都在一行上,所以它看不到它们,并且在我的主函数中将所有字段都返回为 null。如何正确解析 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();
            }
        }

【问题讨论】:

    标签: java xml-parsing jaxb


    【解决方案1】:

    试试:

    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 类。
    3. Employee 中添加了@XmlAttribute 以映射属性名称和getter/setter。
    4. Employees 中添加了@XmlElement 以映射employees 标记和构造函数中的每个employee 元素。

    【讨论】:

    • 你能解释一下为什么用@XmlAttribute 而不是@XmlElement 吗?或者您可以重定向到我可以阅读的来源
    • @Asa @XmlAttribute 在您的情况下用于属性,即idfirstname 等,在您的情况下@XmlElement 表示元素名称,即employee。让我知道它是否适合您
    • 是的,它成功了!我在哪里可以阅读有关它的更多信息以了解何时使用什么?
    猜你喜欢
    • 1970-01-01
    • 2014-02-18
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 2015-06-28
    相关资源
    最近更新 更多