Here you can find the source of createAttribute(Document doc, String nodeName, Map
public static void createAttribute(Document doc, String nodeName, Map<String, String> map)
//package com.java2s; //License from project: Apache License import org.w3c.dom.*; import java.util.Map; public class Main { public static void createAttribute(Document doc, String nodeName, Map<String, String> map) { Node node = doc.getElementsByTagName(nodeName).item(0); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; for (Map.Entry<String, String> entry : map.entrySet()) { element.setAttribute(entry.getKey(), entry.getValue()); }/*from w w w.j a v a2s . co m*/ //TransformerFactory factory = TransformerFactory.newInstance(); //Transformer former = factory.newTransformer(); //former.transform(new DOMSource(doc), new StreamResult(new File("src/shuiguo.xml"))); } } }