This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Strange results in xml parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Strange results in xml parsing " Watch "Strange results in xml parsing " New topic
Author

Strange results in xml parsing

Ranadhir Nag
Ranch Hand

Joined: Mar 09, 2006
Posts: 138
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

Joined: Dec 11, 2004
Posts: 13410

Ranadhir Nag,

You've posted your XML parsing question in the JSP forum.
I'll move it to our XML forum for you.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Strange results in xml parsing