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:
<course name="AdvancedAlgorithm">
<Teacher name="Francis" class="A" />
<Teacher name="John" class="B" />
<Teacher name="Philips" class="C" />
<course name="AlgorithmForBeginners">
<Teacher name="Simon" class="E" />
<Teacher name="Joan" class="F" />
</course>
<Teacher name="Edward" class="M" />
</course>
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?
public void getStructure(NodeList list){
for(int i= 0;i<list.getLength();i++){
Element element = (Element)list.item(i);
if(element.getNodeType()==Node.ELEMENT_NODE && element.getAttribute("name").equals("AdvancedAlgorithm"))
{
NodeList node = element.getChildNodes(); //get the child elements
System.out.println(node.getLength());
for(int k=0; k<node.getLength();k++){
Node currentNode = node.item(i);
Element e = (Element)currentNode;
System.out.println(e.getAttribute("name"));
}
}
}
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