Java XML Element Create createNode(Document doc, Element parent, String nodeName, Object[] attr)

Here you can find the source of createNode(Document doc, Element parent, String nodeName, Object[] attr)

Description

Create a new node with parent element

License

Open Source License

Parameter

Parameter Description
doc the document
parent the parent element
nodeName the node name
attr an array containing attribute name followed by its value: "attr1", 1, "attr2", true, ...

Return

the created element

Declaration

public static Element createNode(Document doc, Element parent, String nodeName, Object[] attr) 

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 {
    /**//w  w  w .j a  v a2 s .co m
     * Create a new node with parent element
     * @param doc the document
     * @param parent the parent element
     * @param nodeName the node name
     * @param attr an array containing attribute name followed by its value: "attr1", 1, "attr2", true, ...
     * @return the created element
     */
    public static Element createNode(Document doc, Element parent, String nodeName, Object[] attr) {
        Element elem = doc.createElement(nodeName);
        for (int i = 0; i < attr.length; i += 2) {
            elem.setAttribute(attr[i].toString(), attr[i + 1].toString());
        }
        parent.appendChild(elem);

        return elem;
    }

    /**
     * Create a new node with parent element
     * @param doc the document
     * @param parent the parent element
     * @param nodeName the node name
     * @param map a map containing {attributes -&gt; value}, the method value.toString() will be used.
     * @return the created element
     */
    public static Element createNode(Document doc, Element parent, String nodeName, Map<String, Object> map) {
        Element elem = doc.createElement(nodeName);
        for (String name : map.keySet()) {
            elem.setAttribute(name, map.get(name).toString());
        }
        parent.appendChild(elem);

        return elem;
    }
}

Related

  1. createElementNsIn(Element parent, String ns, String name, String textContent)
  2. createElementWithText(Element parent, String name, String value)
  3. createElmWithText(Document doc, String tagName, String text)
  4. createNewElementAndSetAndAttribute(Document document, Element parent, String childElement, String childValue, String attributeName, String attributeValue)
  5. createNode(Document doc, Element parent, String nodeName, Object[] attr)
  6. createTable(Element theParent)
  7. createText(final Node parent, final String text)
  8. createText(Node parent, String tag, String text)
  9. createText(String sValue, Node nParent)