Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Removing all text nodes from a given node

Reply
Thread Tools

Removing all text nodes from a given node

 
 
Narokman
Guest
Posts: n/a
 
      01-04-2005
I've written a method that is supposed to remove all text nodes from a
given node and also from its child nodes. The given node is, for
example,
created from following XML:
--------------
<?xml version="1.0" encoding="utf-8" ?>

<L1>L1 Text
<L2>L2 Text
<L3>L3 Text</L3>
</L2>
<L2_1>L2_1 Text</L2_1>
<L2_2>L2_2 Text</L2_2>
</L1>
--------------

The method I've written is :

--------------
public static void removeEmptyText(org.w3c.dom.Node n) {
org.w3c.dom.NodeList nodeList = n.getChildNodes();

for (int i = 0; i < nodeList.getLength(); i++) {
org.w3c.dom.Node node = nodeList.item(i);
int type = node.getNodeType();


// if (type == ELEMENT_TYPE) {
if (node.hasChildNodes()) {
removeEmptyText(node);
}else if (type == TEXT_TYPE) {
n.removeChild(node);
}
}//End of for-loop
}
--------------

This method only works for removing text nodes in the "L1" level.
It fails to remove text nodes in child nodes of "L1".
Any ideas?
If anybody has better way to remove text nodes from a node, I would
also really appreciated.

Thanks.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want to select all the node names beneath a given node thecolour XML 2 06-27-2007 10:46 AM
I want to select all the node names beneath a given node thecolour XML 0 06-26-2007 04:01 PM
select nodes with child node A and child node B gplott@yahoo.com XML 1 11-01-2006 09:27 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
XSLT Select nodes without text-node children whose names starts with specifix text Michael Reiche XML 3 02-05-2004 10:40 PM



Advertisments