• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Java 1.5 and XML Node.

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I use Java 1.5 API. I tried to get the child elements of a xml document by using getChildNodes() method of Node interface. This method reurns a NodeList. The getLength() method of NodeList interface will return the number of child elements in that particular node.

I tried this functionality for same xml node in two different forms. Both the xml documents were part of well formed xml document.

Case 1 :

The Node is Transforms.
The Number of child elements is : 2

<ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms>


Case 2 :

The Node is Transforms. But there is space or new line character between each node.
The Number of child elements is : 5

<ds:Transforms> <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> </ds:Transforms>


Can someone throw light on why the behaviour of the getLength() and item(int index) method of the NodeList interface change for the same Node but in different forms.

Regards,
Arjun.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But there is space or new line character between each node.



ANY space or new line introduces a new Text node. Therefore it is bad practice to depend only on the position of a Node in a NodeList obtained by getChildNodes().

You can use getNodeType() to see if you are looking at a TEXT_NODE or ELEMENT_NODE - the JavaDocs for org.w3c.dom.Node are very useful.

Bill
 
What's that smell? Hey, sniff this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic