我想跳过除 first_name 、node100 及其子节点之外的所有节点。我有这个 XML(实际上我有很多员工,每个员工标签都有很多节点): <employees> <employee> <first_name>John</first_name> <last_name>Doe</last_name> <age>26</age> </employee> <employee> <first_name>Peter</first_name> <last_name>Parker</last_name> <age>30</age> </employee> </employees>我能够使用 Jackson FasterXML 阅读。我创建了 2 个 POJOS 来映射上面的 XML 结构 @JacksonXmlRootElement(localName = "employees") public final class Employees { @JacksonXmlElementWrapper(localName = "employee", useWrapping = false) private Employee[] employee;//ommiteed getter and setterspublic final class Employee { @JacksonXmlProperty(localName = "id", isAttribute = true) private String id; @JacksonXmlProperty(localName = "first_name") private String firstName; @JacksonXmlProperty(localName = "last_name") private String lastName; @JacksonXmlProperty(localName = "age") private int age;现在在生产中,xml 在节点内有 1000 个节点<employee> <first_name>John</first_name> <last_name>Doe</last_name> <age>26</age> <node1> </node1> <node2> </node2> .. <node100> <values> <value> val1 </value> <value> val1 </value> <value> val1 </value> <value> val1 </value> </node100> </employee> <node100> is also inside 4-5 nodes (which i have not shown above).所以我的问题是如何读取first_name、last_name和tag。我的 POJO 类的结构应该是什么?将 XML 转换为 POJO 的代码System.out.println( " hello");ObjectMapper objectMapper = new XmlMapper();// Reads from XML and converts to POJOEmployees employees = objectMapper.readValue( StringUtils.toEncodedString(Files.readAllBytes(Paths.get("C:\\Users\\91895\\Downloads\\File\\XmlFile.xml")), StandardCharsets.UTF_8), Employees.class);System.out.println(employees);
1 回答
浮云间
TA贡献1829条经验 获得超4个赞
在您的类中定义要从 XML 读取的元素。要忽略其他元素,请相应地配置 ObjectMapper:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
添加回答
举报
0/150
提交
取消