File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Strange results in xml parsing " Watch "Strange results in xml parsing " New topic

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"?>

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

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

Joined: Mar 22, 2000
Posts: 13027
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.

I agree. Here's the link:
subject: Strange results in xml parsing
jQuery in Action, 3rd edition