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

MapattributeMap(Element element)
attribute Map
NamedNodeMap n = element.getAttributes();
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < n.getLength(); i++) {
    Attr a = (Attr) n.item(i);
    map.put(a.getName(), a.getValue());
return map;
StringattributeOrNull(Node node, String... attributes)
attribute Or Null
for (int i = 0; i < attributes.length; i++) {
    String attribute = attributes[i];
    Node attr = node.getAttributes().getNamedItem(attribute);
    if (attr != null)
        return attr.getNodeValue();
return null;
StringattributeOrValue(Element element, String mainAttribute, String... alternateAttributes)
Returns the value of one of the attributes, if none is present the text contents of the specified node.
if (element.hasAttribute(mainAttribute))
    return element.getAttribute(mainAttribute);
if (alternateAttributes != null) {
    for (String a : alternateAttributes)
        if (element.hasAttribute(a))
            return element.getAttribute(a);
return element.getTextContent();
...
Listattributes(final Element element)
attributes
final NamedNodeMap attributeMap = element.getAttributes();
if (attributeMap == null || attributeMap.getLength() == 0) {
    return null;
final List<Attr> attributes = new ArrayList<Attr>();
for (int i = 0; i < attributeMap.getLength(); i++) {
    attributes.add((Attr) attributeMap.item(i));
return attributes;
MapattributesAsMap(Element element)
attributes As Map
Map<String, String> result = new LinkedHashMap<>();
NamedNodeMap nodeMap = element.getAttributes();
for (int i = 0, len = nodeMap.getLength(); i < len; i++) {
    Node attrib = nodeMap.item(i);
    String key = localNameOf(attrib);
    String value = attrib.getNodeValue();
    result.put(key, value);
return Collections.unmodifiableMap(result);
ImmutableMapattributesOf(Element element)
attributes Of
NamedNodeMap map = element.getAttributes();
ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
for (int i = 0, size = map.getLength(); i < size; i++) {
    Attr attr = (Attr) map.item(i);
    result.put(attr.getName(), attr.getValue());
return result.build();
StringattributeString(Node node, String attributeName)
attribute String
if (node.hasAttributes()) {
    Node attr = node.getAttributes().getNamedItem(attributeName);
    if (attr != null) {
        return attr.getNodeValue();
return null;
StringattributeStringValue(Element e, String attr, String defaultValue)
attribute String Value
if (!e.hasAttribute(attr)) {
    return defaultValue;
return e.getAttribute(attr);
voidsetAttribute(Element aElement, String aAttributeName, String aValue)
Sets the given attribute value.
if (aValue != null) {
    aElement.setAttribute(aAttributeName, aValue);
voidsetAttribute(Element elem, String name, String value)
Sets an element's attribute (using DOM level 2) with the specified value and namespace prefix.
if (value == null) {
    return;
elem.setAttributeNS(null, name, value);