Query on text and element nodes

A

arvindsd

Hi all,

The code below gets me a list of all the nodes within the node object
called xml_tags_root.

NodeList nl = xml_tags_root.getChildNodes();

The length of nodelist I get is double the number of actual elements!
This is because after every element node there is text node which is (I
think) the whitespace in the XML document.

Is there a way to get only the element nodes?

Please advice.

thanks,
ASD
 
T

Tjerk Wolterink

Hi all,

The code below gets me a list of all the nodes within the node object
called xml_tags_root.

NodeList nl = xml_tags_root.getChildNodes();

The length of nodelist I get is double the number of actual elements!
This is because after every element node there is text node which is (I
think) the whitespace in the XML document.

Is there a way to get only the element nodes?

Well just loop through them and discard the text-nodes.
 
M

Martin Honnen

The code below gets me a list of all the nodes within the node object
called xml_tags_root.

NodeList nl = xml_tags_root.getChildNodes();

The length of nodelist I get is double the number of actual elements!
This is because after every element node there is text node which is (I
think) the whitespace in the XML document.

Is there a way to get only the element nodes?

Depending on the parser and the API you are using there might be ways to
only access the elements nodes, either by having white space text nodes
ignored during parsing and not included in the DOM tree at all or by
using a selection method later, for instance XPath, to access only
element nodes.
But as you have not provided any details on which parser/API you are
using it is hard to tell. I guess you are using Java and the W3C DOM, in
Java 1.5 you could use XPath.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,843
Latest member
WizcraftEntertainmentAgen

Latest Threads

Top