title: Retrieving attributes and values of child Nodes of an XML Element in Java
body: <p>Please, how can I get the attribute value of the second  in the XML course tag name with attribute "AdvancedAlgorithm" in the XML structure below:</p>
  
  <pre><code>&lt;course name="AdvancedAlgorithm"&gt;
      &lt;Teacher name="Francis" class="A" /&gt;
      &lt;Teacher name="John" class="B" /&gt;
      &lt;Teacher name="Philips" class="C" /&gt;
      &lt;course name="AlgorithmForBeginners"&gt;
          &lt;Teacher name="Simon" class="E" /&gt;
          &lt;Teacher name="Joan" class="F" /&gt;
      &lt;/course&gt;
      &lt;Teacher name="Edward" class="M" /&gt;
  &lt;/course&gt;
  </code></pre>
  
  <p>I have tried various means to get it but somehow it gives me first a wrong length and a wrong value.. what am I doing wrong in the code below?</p>
  
  <pre><code> public void getStructure(NodeList list){
  
      for(int i= 0;i&lt;list.getLength();i++){
          Element element = (Element)list.item(i);
             if(element.getNodeType()==Node.ELEMENT_NODE &amp;&amp; element.getAttribute("name").equals("AdvancedAlgorithm"))
           {
                  NodeList node = element.getChildNodes(); //get the child elements
                  System.out.println(node.getLength());
                  for(int k=0; k&lt;node.getLength();k++){
                      Node currentNode = node.item(i);
                      Element e = (Element)currentNode;
                      System.out.println(e.getAttribute("name"));
  
              }
  
           }
      }
  </code></pre>
  
  <p>My analysis:
  The NodeList list has a length of 2. Is that true given that the XML has two course tag but when I assign to the the NodeList node the ChildNodes of the Element element, and checked the lenght of NodeList node, I found out it was 11 instead of 5 since the node has 5 sub nodes. My concern is, first, I want to know what is the length of this XML structure and secondly, how to retrieve the second . Thanks</p>