Java Utililty Methods XML Attribute Set

List of utility methods to do XML Attribute Set

Description

The list of methods to do XML Attribute Set are organized into topic(s).

Method

voidsetAttribute(Node node, String attr, String value)
Sets a named attribute of a Node
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();
...
voidsetAttribute(Node node, String key, String value)
set Attribute
Node attributeNode = node.getOwnerDocument().createAttribute(key);
attributeNode.setNodeValue(value);
node.getAttributes().setNamedItem(attributeNode);
booleansetAttribute(Node pNode, String attrName)
set Attribute
NamedNodeMap name = pNode.getAttributes();
if (name.getLength() > 0) {
    Node thenode = name.getNamedItem(attrName);
    if (thenode != null) {
        thenode.setNodeValue(attrName);
        return true;
    } else {
        return false;
...
voidsetAttribute(String name, String value, Element el)
Sets the value of the given DOM element's attribute with the specified name to the given value.
if (name == null || value == null || el == null)
    return;
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);
voidsetAttributeNS(Element node, String namespaceURI, String prefix, String nodeName, String value)
Adds the named attribute with the given namespace URI and value to the given element node.
String fullName = (prefix == null) ? nodeName : prefix + ":" + nodeName;
node.setAttributeNS(namespaceURI, fullName, value);
if (printXMLNamespaces) {
    String attrName = (prefix == null) ? "xmlns" : "xmlns:" + prefix;
    node.setAttribute(attrName, namespaceURI);
voidsetAttributeValue(Element element, String attribute, String value)
Set an attribute and his value to the specified node
element.setAttribute(attribute, value);
booleansetAttributeValue(final Element target, final String attributeName, final String value)
set Attribute Value
final NamedNodeMap map = target.getAttributes();
final Node attributeValueHolder = map.getNamedItem(attributeName);
if (attributeValueHolder != null) {
    attributeValueHolder.setNodeValue(value);
    return true;
return false;
voidsetAttributeValue(Node node, String attName, String attValue)
set Attribute Value
Node attr = node.getAttributes().getNamedItem(attName);
attr.setNodeValue(attValue);
voidsetAttributeValue(Node node, String attribute, String value)
set Attribute Value
NamedNodeMap attributes = node.getAttributes();
if (attributes != null) {
    Node nameAttr = attributes.getNamedItem(attribute);
    if (nameAttr != null) {
        nameAttr.setNodeValue(value);
voidsetAttributeValue(Node sNode, String attribName, String val)
Utility method to set the attribute value on the given element node
Node attr = sNode.getAttributes().getNamedItem(attribName);
if (attr != null) {
    attr.setNodeValue(val);
} else {
    attr = sNode.getOwnerDocument().createAttribute(attribName);
    attr.setNodeValue(val);
    sNode.getAttributes().setNamedItem(attr);