Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange results in xml parsing

 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a very simple xml
<?xml version="1.0"?>
<root>
<control>
<element>name</element>
<elementtype>input</elementtype>
<subtype>text</subtype>
</control>
</root>

And a simple parsing code using the org.w3c.dom package(1.4.2 java):
//get a nodelist formed by getelementbytagname("control")
NodeList list=utils.getList(filter);

System.out.println(list.getLength());//prints 1 which is correct

for(int i=0;i<list.getLength(); i++) {
Element node = (Element)list.item(i);
System.out.println("Name=>" + node.getTagName());//prints 'control' which is correct

NodeList childlist=node.getChildNodes();

System.out.println(childlist.getLength());//print 7!!!

}

Why is the last iteration printing 7?
How do i print out the name and value of the children of 'control'?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranadhir Nag,

You've posted your XML parsing question in the JSP forum.
I'll move it to our XML forum for you.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the last iteration printing 7

Because there are 7 nodes - you forgot the TEXT type nodes containing crlf between the Elements.

I suggest you go to Sun's DOM tutorial for ideas on getting the name and value of DOM nodes.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic