Here you can find the source of setElementText(Element elm, String text)
static public void setElementText(Element elm, String text)
//package com.java2s; /*/*from www.ja va 2s.c o m*/ * Copyright 2004-2014 SmartBear Software * * Licensed under the EUPL, Version 1.1 or - as soon as they will be approved by the European Commission - subsequent * versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * * http://ec.europa.eu/idabc/eupl * * Unless required by applicable law or agreed to in writing, software distributed under the Licence is * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the Licence for the specific language governing permissions and limitations * under the Licence. */ import org.w3c.dom.CDATASection; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.ProcessingInstruction; import org.w3c.dom.Text; public class Main { static public void setElementText(Element elm, String text) { Node node = elm.getFirstChild(); if (node == null) { if (text != null) { elm.appendChild(elm.getOwnerDocument().createTextNode(text)); } } else if (node.getNodeType() == Node.TEXT_NODE) { if (text == null) { node.getParentNode().removeChild(node); } else { node.setNodeValue(text); } } else if (text != null) { Text textNode = node.getOwnerDocument().createTextNode(text); elm.insertBefore(textNode, elm.getFirstChild()); } } public static boolean setNodeValue(Node domNode, String string) { if (domNode == null) { return false; } short nodeType = domNode.getNodeType(); switch (nodeType) { case Node.ELEMENT_NODE: { setElementText((Element) domNode, string); break; } case Node.ATTRIBUTE_NODE: case Node.TEXT_NODE: { domNode.setNodeValue(string); break; } case Node.PROCESSING_INSTRUCTION_NODE: { ((ProcessingInstruction) domNode).setData(string); break; } case Node.CDATA_SECTION_NODE: { ((CDATASection) domNode).setData(string); break; } default: { return false; } } return true; } }