How to read the values from a XML file using Java when all the tags are same..?
<p>My XML looks something like below</p>

<pre><code>&lt;var id="attr1"&gt;
      &lt;attr1&gt;
         &lt;var id="key1"&gt;value1&lt;/var&gt;
         &lt;var id="key2"&gt;value2&lt;/var&gt;
         &lt;var id="key3"&gt;value3&lt;/var&gt;
      &lt;/attr1&gt;
&lt;/var&gt;

&lt;var id="attr2"&gt;
      &lt;attr2&gt;
         &lt;var id="key1"&gt;value4&lt;/var&gt;
         &lt;var id="key2"&gt;value5&lt;/var&gt;
         &lt;var id="key3"&gt;value6&lt;/var&gt;
         &lt;var id="key4"&gt;value7&lt;/var&gt;
      &lt;/attr2&gt;
&lt;/var&gt;
</code></pre>

<p>I am  trying to get the values (which will be unique) from the above xml. The key names can be same and so are the tag names (in this case it is "var"), which is making it challenging while getting the values. The attribute names are unique as well. If I use the below code </p>

<pre><code>NodeList nList = doc.getElementsByTagName("var");               
for (int temp = 0; temp &lt; nList.getLength(); temp++) {
    Node nNode = nList.item(temp);
    System.out.println("Current Element :" + nNode.getNodeName());
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode;
        System.out.println("Parent id : "
                        + eElement.getAttribute("id"));
    }
}
</code></pre>

<p>If I execute the above method, I am getting all the keys. Is there a way to get the value of a specific key..? How to actually traverse to get to the value (say value1)..?</p>