Java tutorial
//package com.java2s; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; public class Main { /** * Add an attribute to an Element. * @param doc XML document * @param element XML Element * @param attribute attribute name * @param value attribute value */ public static void addAttribute(final Document doc, final Element element, final String attribute, final String value) { final NamedNodeMap atts = element.getAttributes(); final Attr newAttr = doc.createAttribute(attribute); newAttr.setValue(value); atts.setNamedItem(newAttr); } }