Here you can find the source of setText(Node node, String text)
Parameter | Description |
---|---|
node | Node. |
text | New text for the node. |
public static void setText(Node node, String text)
//package com.java2s; /* Please see the license information at the end of this file. */ import org.w3c.dom.*; public class Main { /** Sets text for a node. */*from ww w . j a v a2 s . c o m*/ * <p>Sets the value of the first child text node, if any. * Creates new child text node if none found.</p> * * @param node Node. * * @param text New text for the node. */ public static void setText(Node node, String text) { NodeList children = node.getChildNodes(); int numChildren = children.getLength(); for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (child.getNodeType() == Node.TEXT_NODE) { child.setNodeValue(text); return; } } Text child = node.getOwnerDocument().createTextNode(text); node.appendChild(child); } }