Here you can find the source of setElementsTextNodeValue(Node element, String value)
public static void setElementsTextNodeValue(Node element, String value)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static void setElementsTextNodeValue(Node element, String value) { NodeList children = element.getChildNodes(); List<Node> childs_to_remove = new ArrayList<Node>(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child != null && child.getNodeType() == Node.TEXT_NODE) childs_to_remove.add(child); }/*from w w w .j ava2 s.c o m*/ for (Iterator<Node> iter = childs_to_remove.iterator(); iter.hasNext();) element.removeChild(iter.next()); Node text_node = element.getOwnerDocument().createTextNode(value); element.appendChild(text_node); } }