0

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 !

Nomesh DeSilva
  • 1,649
  • 4
  • 25
  • 43

1 Answers1

0

The code above correctly adds 2 entries in the map. The first entry, maps element Form to it's text content (which is the collection of the text content of all it's descendant nodes).

If you want to access the descendant nodes you'll need to use eElement.getChildNodes() and iterate over the NodeList returned.

This might be useful: Java: Most efficient method to iterate over all elements in a org.w3c.dom.Document?

localghost
  • 419
  • 2
  • 6