• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XML parsing using DOM

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without seeing where that NodeList came from it's impossible to tell what the output of the code should be. And you don't say what the output is, either.

I'm going to guess that you're making the standard beginner error of not realizing that whitespace text nodes are also nodes. So for example your <root> element has three children:

1. A text node containing a newline character.
2. A <parent> element.
3. Another text node containing a newline character.
 
reply
    Bookmark Topic Watch Topic
  • New Topic