Here you can find the source of setText(Node n, String text)
public synchronized static void setText(Node n, String text)
//package com.java2s; import org.w3c.dom.*; public class Main { /**//from www.j a va 2s .c om * Sets the text for a node */ public synchronized static void setText(Node n, String text) { if (n == null) throw new IllegalArgumentException("Node argument cannot be null"); if (text == null) throw new IllegalArgumentException("Node text argument cannot be null"); NodeList nl = n.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == Node.TEXT_NODE) { nl.item(i).setNodeValue(text); return; } } Node textNode = n.getOwnerDocument().createTextNode(text); n.appendChild(textNode); } }