Here you can find the source of copyAttributes(Node from, Node to)
public static void copyAttributes(Node from, Node to)
//package com.java2s; //License from project: Open Source License import org.w3c.dom.Attr; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; public class Main { public static void copyAttributes(Node from, Node to) { NamedNodeMap map = from.getAttributes(); for (int i = 0; i < map.getLength(); i++) { Node attr = map.item(i); addAttribute(to, attr.getNodeName(), attr.getNodeValue()); }//from w ww . j a v a2 s. c om } public static Attr addAttribute(Node parent, String name, String value) { if (value == null) return null; Attr node = parent.getOwnerDocument().createAttribute(name); try { node.setValue(value); parent.getAttributes().setNamedItem(node); } catch (Exception e) { System.out.println("Problem rewriting " + parent.getNodeName() + "." + name + "='" + node.getValue() + "' -> '" + value + "'"); } return node; } }