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

GNU General Public 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;
/*/* w  w  w. jav a 2s  .c  om*/
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2010 - Calixte DENIZET
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 */

import java.util.Map;

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

public class Main {
    /**
     * 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 (Map.Entry<String, Object> name : map.entrySet()) {
            elem.setAttribute(name.getKey(), name.getValue().toString());
        }
        parent.appendChild(elem);

        return elem;
    }
}

Related

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