Java XML Attribute Set setAttribute(String name, String value, Element el)

Here you can find the source of setAttribute(String name, String value, Element el)

Description

Sets the value of the given DOM element's attribute with the specified name to the given value.

License

Open Source License

Declaration

public static void setAttribute(String name, String value, Element el) 

Method Source Code

//package com.java2s;

import org.w3c.dom.*;

public class Main {
    /**/*from   w  w  w . j  a  v a 2  s .  com*/
     * Sets the value of the given DOM element's attribute
     * with the specified name to the given value.
     */
    public static void setAttribute(String name, String value, Element el) {
        if (name == null || value == null || el == null)
            return;

        // strip out invalid characters from the value
        char[] v = value.toCharArray();
        int count = 0;
        for (int i = 0; i < v.length; i++) {
            if (!Character.isISOControl(v[i]))
                count++;
        }
        if (count < v.length) {
            char[] nv = new char[count];
            count = 0;
            for (int i = 0; i < v.length; i++) {
                if (!Character.isISOControl(v[i]))
                    nv[count++] = v[i];
            }
            value = new String(nv);
        }

        el.setAttribute(name, value);
    }

    /**
     * Sets the value of the given DOM element's attribute with the
     * specified name to the given value's string representation.
     */
    public static void setAttribute(String name, Object value, Element el) {
        setAttribute(name, value == null ? null : value.toString(), el);
    }
}

Related

  1. setAttribute(Node n, String name, String value)
  2. setAttribute(Node node, String attName, String val)
  3. setAttribute(Node node, String attr, String value)
  4. setAttribute(Node node, String key, String value)
  5. setAttribute(Node pNode, String attrName)
  6. setAttributeNS(Element node, String namespaceURI, String prefix, String nodeName, String value)
  7. setAttributeValue(Element element, String attribute, String value)
  8. setAttributeValue(final Element target, final String attributeName, final String value)
  9. setAttributeValue(Node node, String attName, String attValue)