I want to skip all nodes except first_name ,node100 and its children. I have this XML (In reality I have many employee and each employee tag has many nodes):
<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>
I am able to read the using Jackson FasterXML. I have created 2 POJOS to map above XML structure
@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;
Now in production the xml have 1000s nodes inside node
<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).
So my question is how can I just read first_name , last_name, and tag . What should be the structure of my POJO class ?
Code to convert XML to 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);
In your class define the elements that you want to read from XML. To ignore other elements configure ObjectMapper correspondingly:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);