Java tutorial
//package com.java2s; //License from project: Open Source License import org.w3c.dom.*; public class Main { /** * Adds an {@link Element} child to the given element. * <p> * If you only want to generate elements for a document that is to be * serialized, this method is fine. If, however, you want to manipulate * the document, you should probably use * {@link #addElementChildTo(Element,String,String)}. * * @param parent The {@link Element} to add the {@link Element} child to. * @param tagName The tag name of the new {@link Element} child. * @return Returns A new {@link Element} with its <code>nodeName</code> set * to <code>tagName</code>, and <code>localName</code>, <code>prefix</code>, * and <code>namespaceURI</code> set to <code>null</code>. * @see #addElementChildTo(Element,String,String) */ public static Element addElementChildTo(Element parent, String tagName) { final Document doc = parent.getOwnerDocument(); final Element child = doc.createElement(tagName); parent.appendChild(child); return child; } /** * Adds an {@link Element} child to the given element. * * @param parent The {@link Element} to add the {@link Element} child to. * @param qualifiedName The fully qualified name of the new {@link Element} * child. * @return Returns the new {@link Element} child. */ public static Element addElementChildTo(Element parent, String nsURI, String qualifiedName) { final Document doc = parent.getOwnerDocument(); final Element child = doc.createElementNS(nsURI, qualifiedName); parent.appendChild(child); return child; } }