Java tutorial
//package com.java2s; import org.w3c.dom.Attr; import org.w3c.dom.Node; public class Main { public static Node addAttribute(Node pNode, String attrName, String attrValue) { Node attributeNode = null; try { Attr _attr = pNode.getOwnerDocument().createAttribute(attrName); _attr.setNodeValue(attrValue); attributeNode = pNode.getAttributes().setNamedItem(_attr); } catch (Exception e) { attributeNode = null; } return attributeNode; } }