Java tutorial
//package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { public static void appendElementText(Element parentElement, String elementName, String elementValue) { if (parentElement == null || elementName == null || elementValue == null) throw new NullPointerException(); Element childElement = parentElement.getOwnerDocument().createElement(elementName); setTextContent(childElement, elementValue); parentElement.appendChild(childElement); } public static void setTextContent(Node node, String nodeValue) { if (node == null || nodeValue == null) throw new NullPointerException(); node.appendChild(node.getOwnerDocument().createTextNode(xmlEncode(nodeValue))); } public static String xmlEncode(String string) { return string.replace("&", "&").replace("'", "'").replace("\"", """).replace("<", "<") .replace(">", ">"); } }