Here you can find the source of setTextValue(Element element, String text)
Parameter | Description |
---|---|
element | Element whose text element we want |
text | Text to set |
public static void setTextValue(Element element, String text)
//package com.java2s; //License from project: Apache License import org.w3c.dom.NodeList; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** Replace the first text element of the given element with a new text element containing the given text or just create a new text element if there is none to replace. // www . j a v a2 s . c o m @param element Element whose text element we want @param text Text to set */ public static void setTextValue(Element element, String text) { Node newTextNode = element.getOwnerDocument().createTextNode(text); NodeList childs = element.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { Node node = childs.item(i); if (Node.TEXT_NODE == node.getNodeType()) { element.replaceChild(newTextNode, node); return; } } element.appendChild(newTextNode); } }