如何从XML文件解析数据创建Employee类对象数组
解决方案
核心问题说明
你调用getAttributeValue()无法获取值的根本原因是你的XML结构中员工信息是*<employee id="1" firstName="John">的属性写法才能通过属性相关方法获取值。
方案1:基于现有DOM代码改造
第一步:先定义Employee POJO类
public class Employee { private Integer id; private String firstName; private String lastName; private String country; private Integer age; // 无参构造、全参构造、getter方法自行补充 public Employee() {} public void setId(Integer id) { this.id = id; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setCountry(String country) { this.country = country; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Employee{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", country='" + country + '\'' + ", age=" + age + '}'; } }
第二步:修改DOM解析逻辑
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = documentBuilder.parse("data.xml"); Node root = document.getDocumentElement(); NodeList staff = root.getChildNodes(); // 定义集合存储所有解析出的Employee对象 List<Employee> employeeList = new ArrayList<>(); for (int i = 0; i < staff.getLength(); i++) { Node employeeNode = staff.item(i); if (employeeNode.getNodeType() != Node.TEXT_NODE && "employee".equals(employeeNode.getNodeName())) { Employee employee = new Employee(); NodeList employeeProps = employeeNode.getChildNodes(); for (int j = 0; j < employeeProps.getLength(); j++) { Node propNode = employeeProps.item(j); if (propNode.getNodeType() != Node.TEXT_NODE) { String propName = propNode.getNodeName(); String propValue = propNode.getTextContent().trim(); // 按节点名称赋值给Employee对应字段 switch (propName) { case "id": employee.setId(Integer.parseInt(propValue)); break; case "firstName": employee.setFirstName(propValue); break; case "lastName": employee.setLastName(propValue); break; case "country": employee.setCountry(propValue); break; case "age": employee.setAge(Integer.parseInt(propValue)); break; } } } employeeList.add(employee); } } // 测试输出结果 employeeList.forEach(System.out::println);
方案2:更简便的JAXB实现(无需手动遍历DOM)
JAXB是Java内置的XML绑定工具,可直接实现XML和Java对象的自动转换,代码更简洁:
第一步:给实体类加JAXB注解
import jakarta.xml.bind.annotation.XmlElement; import jakarta.xml.bind.annotation.XmlRootElement; // Java 8及更早版本使用javax.xml.bind.annotation包下的注解 @XmlRootElement(name = "employee") public class Employee { private Integer id; private String firstName; private String lastName; private String country; private Integer age; @XmlElement public void setId(Integer id) { this.id = id; } @XmlElement public void setFirstName(String firstName) { this.firstName = firstName; } @XmlElement public void setLastName(String lastName) { this.lastName = lastName; } @XmlElement public void setCountry(String country) { this.country = country; } @XmlElement public void setAge(Integer age) { this.age = age; } // 剩余getter、构造方法自行补充 } // 定义根节点对应的Staff类 @XmlRootElement(name = "staff") class Staff { private List<Employee> employees; @XmlElement(name = "employee") public void setEmployees(List<Employee> employees) { this.employees = employees; } public List<Employee> getEmployees() { return employees; } }
第二步:直接解析XML文件
JAXBContext context = JAXBContext.newInstance(Staff.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Staff staff = (Staff) unmarshaller.unmarshal(new File("data.xml")); List<Employee> employeeList = staff.getEmployees(); // 直接获取到所有解析完成的Employee对象
内容的提问来源于stack exchange,提问作者Viktor Loskutov
相关产品推荐
相关产品推荐

