Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.Map; import java.util.Map.Entry; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** * Convenience function for creating elements. * * @param document * the DOM document we're creating the element in * @param name * the tagname for the element * @param attributes * a map from attribute names to attributes, or <CODE>null</CODE> * if this element should have no attributes * @param text * the text for the element, which will be made into a text node * and added as a child of the created element, or <CODE>null</CODE> * if the element should have no children * @return a new element */ public static Element createElement(Document doc, String name, Object text, Map<String, Object> attributes) { Element e = doc.createElement(name); // Set the attributes. if (attributes != null) { for (Entry<String, Object> entry : attributes.entrySet()) { e.setAttribute(entry.getKey(), entry.getValue().toString()); } } // Add the text element. if (text != null) e.appendChild(createTextNode(doc, text.toString())); return e; } public static Node createTextNode(Document doc, String string) { return doc.createTextNode(string); } }