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

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

Description

create an element from a document, that has some text content in it

License

Open Source License

Parameter

Parameter Description
doc - document
name - element name
attributes - attribute map

Return

element object

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
    /**//from  w  ww  .j a  v a 2 s  .co  m
     * create an element from a document, that has some text content in it
     * @param doc - document
     * @param name - element name
     * @param content - text content
     * @return element object
     */
    public static Element createElement(Document doc, String name, String content) {
        Element e = doc.createElement(name);
        e.appendChild(doc.createTextNode(content));
        return e;
    }

    /**
     * create an element from a document, that has some text content in it
     * @param doc - document
     * @param name - element name
     * @param attribute - attribute
     * @param value - value
     * @return element object
     */
    public static Element createElement(Document doc, String name, String attribute, String value) {
        Element e = doc.createElement(name);
        e.setAttribute(attribute, value);
        return e;
    }

    /**
     * create an element from a document, that has some text content in it
     * @param doc - document
     * @param name - element name
     * @param attributes - attribute map
     * @return element object
     */
    public static Element createElement(Document doc, String name, Map<String, String> attributes) {
        Element e = doc.createElement(name);
        for (String attribute : attributes.keySet())
            e.setAttribute(attribute, attributes.get(attribute));
        return e;
    }

    /**
     * create an element from a document, that has some text content in it
     * @param doc - document
     * @param name - element name
     * @param attributes - attribute map
     * @param content - string content
     * @return element object
     */
    public static Element createElement(Document doc, String name, Map<String, String> attributes, String content) {
        Element e = createElement(doc, name, attributes);
        e.appendChild(doc.createTextNode(content));
        return e;
    }
}

Related

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