Java XML Element Create createElement(Document doc, String name, Object text, Map attributes)

Here you can find the source of createElement(Document doc, String name, Object text, Map attributes)

Description

Convenience function for creating elements.

License

Open Source License

Parameter

Parameter Description
document the DOM document we're creating the element in
name the tagname for the element
attributes a map from attribute names to attributes, or <CODE>null</CODE> if this element should have no attributes
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

Declaration

public static Element createElement(Document doc, String name, Object text, Map<String, Object> attributes) 

Method Source Code

//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 {
    /**//  www .  j a va2s  .  c  o m
     * 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);
    }
}

Related

  1. createElement(Document doc, Node parent, int depth, String name, String contents)
  2. createElement(Document doc, Node parent, String tagName)
  3. createElement(Document doc, String elementNamespace, String elementName)
  4. createElement(Document doc, String expr, Element parentElement, String elementName, boolean firstChild, Map attributes)
  5. createElement(Document doc, String name, Map attributes)
  6. createElement(Document doc, String name, String prefix, String namespaceURI)
  7. createElement(Document doc, String tag, String data)
  8. createElement(Document doc, String tag, String nsURI, String prefix)
  9. createElement(Document doc, String tagName)