posted 18 years ago
Consider an XML document as follows:
<root>
<parent>
<child1>
<subchild1>name1</subchild1>
<subchild2>name2</subchild2>
</child1>
<child2>
<subchild3>name1</subchild1>
<subchild4>name2</subchild2>
</child2>
</parent>
</root>
I have written the following recursive function to retrieve the elements and the corresponding value in a node list.
private void EvaluateNode(NodeList nl4)
{
System.out.println("------->> " + nl4.getLength() + nl4.item(0).getNodeName());
//System.out.println("value >> " + nl4.item(0).hasChildNodes());
if(nl4.item(0).hasChildNodes())
{
NodeList nl5 = nl4.item(0).getChildNodes();
for(int i=0;i<nl5.getLength();i++)
{
if(nl5.item(i).getNodeType() == Node.ELEMENT_NODE)
{
System.out.println("Child True Node " + nl5.item(i).getNodeName() );
EvaluateNode(nl5);
}
}
}
else
{
if(nl4.item(0).getNodeType() == Node.ELEMENT_NODE)
{
if(nl4.item(0).getAttributes().getLength()>0)
{
for(int l=0;l<nl4.item(0).getAttributes().getLength();l++)
System.out.println("Tag Name >> " + nl4.item(0).getNodeName() + " << Value >>" + nl4.item(0).getTextContent() + " >> Attr " + nl4.item(0).getAttributes().item(l).getTextContent());
}
}
else
System.out.println("Tag Name >> " + nl4.item(0).getNodeName() + " << Value >>" + nl4.item(0).getTextContent());
}
}
Problem:
--------
I am not able to get the subchild tag name i.e subchild1,subchild2,subchild3,subchild4.
I have used nl4.item(0).getNodeName() for the same.
Can anybody suggest me if my approach is right!
Advance Thanks.