我想跳过除 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 setters
public 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 POJO
Employees 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);
浮云间
相关分类