Java tutorial
//package com.java2s; //License from project: Open Source License import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static void setChildText(Element e, String tagname, String newValue) { setChildText(e, tagname, newValue, false); } public static void setChildText(Element e, String tagname, String newValue, boolean cdata) { Element child = getChildByTagName(e, tagname); if (child == null) { Document doc = e.getOwnerDocument(); child = doc.createElement(tagname); e.appendChild(child); } setElementText(child, newValue, cdata); } public static Element getChildByTagName(Element e, String tagname) { if (e == null) { return null; } NodeList children = e.getElementsByTagName(tagname); if ((children == null) || (children.getLength() == 0)) { return (null); } return ((Element) children.item(0)); } public static void setElementText(Element e, String newValue) { setElementText(e, newValue, false); } /** * For compatibility reasons the following is required: * If the value of a text node is to be changed, but a CDATA section with this name * already exists, the CDATA section is removed an a text node is created or changed. * * If the value of a CDATA section is to be changed, but a text node with this name * already exists, the text node is removed an a CDATA section is created or changed. * */ public static void setElementText(Element e, String newValue, boolean cdata) { if (e == null) { return; } Node node = null; NodeList children = e.getChildNodes(); if (children != null) { Node childToRemove = null; boolean changed = false; int listLength = children.getLength(); for (int i = 0; i < listLength; i++) { node = children.item(i); int nodeType = node.getNodeType(); if (nodeType == Node.TEXT_NODE) { if (cdata) { childToRemove = node; } else { node.setNodeValue(newValue); changed = true; } } if (nodeType == Node.CDATA_SECTION_NODE) { if (!cdata) { childToRemove = node; } else { node.setNodeValue(newValue); changed = true; } } } if (childToRemove != null) { // System.out.println("removing child " + childToRemove.getNodeValue()); childToRemove.setNodeValue(""); e.removeChild(childToRemove); } if (changed) { return; } } Document doc = e.getOwnerDocument(); if (cdata) { node = doc.createCDATASection(newValue); } else { node = doc.createTextNode(newValue); } e.appendChild(node); } }