I have an XML file that i need to map with a POJO. My XML looks like this :
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<contentType>Document</contentType>
<siteName>mySite</siteName>
<listName>myLib</listName>
<folderName>docset-folder</folderName>
<documentSetName>documentSet</documentSetName>
<fields>
<field name="META_1">123456789</field>
<field name="META_2">Someone</field>
<field name="META_3">Germany</field>
</fields>
</data>
My POJO looks like this:
public class MetaData implements Serializable {
private String contentType;
private String siteName;
private String listName;
private String folderName;
private String documentSetName;
private Map<String, Object> fields;
}
My objective is to be able to map any XML with any POJO, for this I found how to list the members of a class. At this stage I have a method that allow to iterate a nodelist.
public static Iterable<Node> iterable(final NodeList nodeList) {
return () -> new Iterator<Node>() {
private int index = 0;
@Override
public boolean hasNext() {
return index < nodeList.getLength();
}
@Override
public Node next() {
if (!hasNext())
throw new NoSuchElementException();
return nodeList.item(index++);
}
};
}
And a main method where I try to use DocumentBuilderFactory:
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
String path = "src/test/resources/bill.xml";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// list the members of the metadata class
List<Field> fields = Arrays.asList(MetaData.class.getDeclaredFields());
try {
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(path));
doc.getDocumentElement().normalize();
System.out.println("Root Element :" + doc.getDocumentElement().getNodeName());
System.out.println("------");
NodeList list = doc.getElementsByTagName("data");
iterable(list).forEach(node -> {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
fields.forEach(field -> {
if(element.getAttribute(field.getName()).length() > 0) {
System.out.println(element.getAttribute(field.getName()));
} else {
System.out.print(element.getElementsByTagName(field.getName()).item(0).getNodeName());
System.out.println(" - " + element.getElementsByTagName(field.getName()).item(0).getTextContent());
}
});
}
// System.out.println(node.getTextContent());
});
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
I'm here just testing to get the XML properties, attributes and values. This code gives me the following result :
contentType - Document
siteName - mySite
listName - myLib
folderName - docset-folder
documentSetName - documentSet
fields -
123456789
Someone
Germany
My issue is that : i would expect the following :
contentType - Document
siteName - mySite
listName - myLib
folderName - docset-folder
documentSetName - documentSet
fields -
META_1 - 123456789
META_2 - Someone
META_3 - Germany
My objective would be to be able to map it with a MetaData object. fields here should be added to a map with META_1 as key and 123456789 as value. I'm a little bit stuck here.