First you need to write some Java classes modeling your XML content.
The classes get @JacksonXml...
annotations to tell Jackson the mapping between XML and Java.
These annotations are especially important when a Java name is different from the XML name.
One class is for representing the <BESAPI> root element:
@JacksonXmlRootElement(localName = "BESAPI")
public class BESAPI {
@JacksonXmlProperty(isAttribute = true, localName = "noNamespaceSchemaLocation", namespace = XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)
private String noNamespaceSchemaLocation;
@JacksonXmlProperty(isAttribute = false, localName = "Employee")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Employee> employees;
// public getters and setters (omitted here for brevity)
}
and another class for representing the <Employee> element
public class Employee {
@JacksonXmlProperty(isAttribute=true, localName="Resource")
private String resource;
// public getters and setters (omitted here for brevity)
}
Then you can use Jackson's XmlMapper for reading XML content.
XmlMapper xmlMapper = new XmlMapper();
File file = new File("example.xml");
BESAPI besApi = xmlMapper.readValue(file, BESAPI.class);
for (Employee employee : besApi.getEmployees()) {
System.out.println(employee.getResource());
}
xsi:prefix without defining it. You need to addxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"to theBESAPIroot element.