Here you can find the source of createNode(Document doc, Element parent, String nodeName, Object[] attr)
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, ... |
public static Element createNode(Document doc, Element parent, String nodeName, Object[] attr)
//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 -> 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; } }