Here you can find the source of setText(Node node, String text)
public static Text setText(Node node, String text)
//package com.java2s; /******************************************************************************* * Copyright (C) 2006-2013 AITIA International, Inc. * /*from w ww.j a v a 2 s .c o m*/ * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; public class Main { /** * Creates a Text child, or replaces all existing children of type Text. * Non-Text descendants are preserved without change. * @return The Text child which receives the specified text. * @see Node.setTextContent() */ public static Text setText(Node node, String text) { NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0, n = children.getLength(); i < n; ++i) { Node child = children.item(i); if (child instanceof Text) { return ((Text) child).replaceWholeText(text); } } } Text ans = node.getOwnerDocument().createTextNode(text); node.appendChild(ans); return ans; } }