/***** Code de MesExemples.com *******/
/**
* @(#)XMLCleanNodeText.java
*
*
* @author
*sakoba(java.mesexemples.com) @version 1.00 2013/7/4
*/
import java.util.Vector;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* @author Abey Mullassery
*
*/
public class XMLCleanNodeText
{
public static void cleanText(Node node) {
try {
NodeList childNodes = node.getChildNodes();
int noChildren = childNodes.getLength();
Node n = null;
short type = 0;
Vector rem = new Vector();
for (int i = 0; i < noChildren; i++) {
n = childNodes.item(i);
type = n.getNodeType();
if (type == Node.TEXT_NODE) {
rem.add(n);
} else if (type == Node.ELEMENT_NODE) {
cleanText(n);
}
}
for (int i = 0; i < rem.size(); i++) {
node.removeChild((Node) rem.get(i));
}
} catch (Exception e) {
}
}
}
Code testé avec le fichier XML Suivant
Sakoba
Adams
Rappel
Ne m'oubliez pas ce week-end!