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; //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 -> 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; } }