This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android 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: 12761
    
    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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange results in xml parsing
 
Similar Threads
help with web service to display xml file contents using netbeans
attributes
Why is this happening?
Unable to get the correct data from the XML file
getting the value of a node