Java tutorial
//package com.java2s; import org.w3c.dom.*; public class Main { public static Element addElement(Node parent, String name, Attr[] attrs) { Element element; if (parent instanceof Document) { element = ((Document) parent).createElement(name); } else { element = parent.getOwnerDocument().createElement(name); } if (attrs != null && attrs.length > 0) { for (Attr attr : attrs) { element.setAttributeNode(attr); } } parent.appendChild(element); return element; } public static Element addElement(Element parent, Element childElement) { Node childNode = parent.getOwnerDocument().importNode(childElement, true); parent.appendChild(childNode); return parent; } }