I am reading a dynamic XML file (without any known structure) and putting the relevant tag name and value to a hashmap (ex: metadata<tagName, Value>
).
My issue here is, I can not get the tagName but it only adds the root tagName and all the values of entire xml.
my XML is:
<?xml version="1.0" encoding="UTF-8"?>
<form kwf="VARA">
<sec1>
<docID>2d2c5bf209b79d8b1a1f840ce4ce4030e66a76d6</docID>
<qrCode>xx.jpg</qrCode>
<title>NOOO FORM NAME</title>
<ELO_VARAFNAME>NO</ELO_VARAFNAME>
<ELO_VARALNAME>NAME</ELO_VARALNAME>
<ELO_VARAEMAIL>noname@gmail.com</ELO_VARAEMAIL>
<ELO_VARAORBEONDOCID>2d2c5bf209b79d8b1a1f840ce4ce4030e66a76d6</ELO_VARAORBEONDOCID>
</sec1>
</form>
My Code is:
public static Map<String,String> getMetaDataFromOrbeonXML(File fXmlFile) throws SAXException, ParserConfigurationException, IOException
{
Map metaData = new HashMap();
String formName="";
String docID = "";
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("form");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
docID = eElement.getElementsByTagName("docID").item(0).getTextContent();
metaData.put("docID", docID);
metaData.put("appName", APP_NAME);
metaData.put(eElement.getTagName(), eElement.getTextContent());
System.out.println("META DATA MAP: "+ metaData.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return metaData;
}
And the out put is:
{form= 2d2c5bf209b79d8b1a1f840ce4ce4030e66a76d6
xx.jpg
NOOO FORM NAME
NO
NAME
noname@gmail.com
2d2c5bf209b79d8b1a1f840ce4ce4030e66a76d6
, docID=2d2c5bf209b79d8b1a1f840ce4ce4030e66a76d6, appName=VIRGINAUSI, formName=AITSLForm}
Tag names are missing in the map except the root element. Please help !