Java XML Attribute Set setAttribute(Node node, String attr, String value)

Here you can find the source of setAttribute(Node node, String attr, String value)

Description

Sets a named attribute of a Node

License

Open Source License

Parameter

Parameter Description
node The node
attr The name of the attribute to set
value The value to assign to the attribute

Declaration

public synchronized static void setAttribute(Node node, String attr, String value) 

Method Source Code


//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**//from  w  w w. j  ava 2s. c  om
     *  Sets a named attribute of a Node
     *  @param node The node
     *  @param attr The name of the attribute to set
     *  @param value The value to assign to the attribute
     */
    public synchronized static void setAttribute(Node node, String attr, String value) {
        if (node == null)
            throw new IllegalArgumentException("Node argument cannot be null");
        if (attr == null)
            throw new IllegalArgumentException("Node attribute argument cannot be null");
        if (value == null)
            throw new IllegalArgumentException("Node attribute value argument cannot be null");

        Node attrN = null;
        NamedNodeMap map = node.getAttributes();
        if (map != null)
            attrN = map.getNamedItem(attr);

        if (attrN == null) {
            attrN = node.getOwnerDocument().createAttribute(attr);
            map.setNamedItem(attrN);
        }

        attrN.setNodeValue(value);
    }
}

Related

  1. setAttribute(Element targetElem, String name, String value)
  2. setAttribute(final Element element, final String attrName, final Object value)
  3. setAttribute(final Element element, final String name, final String value)
  4. setAttribute(Node n, String name, String value)
  5. setAttribute(Node node, String attName, String val)
  6. setAttribute(Node node, String key, String value)
  7. setAttribute(Node pNode, String attrName)
  8. setAttribute(String name, String value, Element el)
  9. setAttributeNS(Element node, String namespaceURI, String prefix, String nodeName, String value)