Java Utililty Methods XML Attribute Get

List of utility methods to do XML Attribute Get

Description

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

Method

StringgetAttributeByName(Node content, String attributeName)
Gets an attribute value by name, returning null if not found
if (content != null) {
    NamedNodeMap atts = content.getAttributes();
    if (atts == null) {
        return null;
    Node att = atts.getNamedItem(attributeName);
    if (att != null) {
        return att.getNodeValue();
...
booleangetAttributeByName(Node node, String name, boolean defaultValue)
Returns the value of a named node attribute in the form of a boolean
if (node == null || name == null)
    return defaultValue;
try {
    return getAttributeBooleanByName(node.getAttributes(), name);
} catch (Exception e) {
    return defaultValue;
StringgetAttributeByName(Node node, String name, String defaultValue)
Returns the attribute value for the passed name in the passed node.
try {
    String val = getAttributeValueByName(node, name);
    if (val != null && !val.trim().isEmpty())
        return val.trim();
} catch (Exception e) {
return defaultValue;
OptionalgetAttributeContent(Node item, String attributeName)
Gets the content of an attribute.
NamedNodeMap attributes = item.getAttributes();
return Optional.ofNullable(attributes.getNamedItem(attributeName)).map(Node::getTextContent);
TgetAttributeEnum(Node node, String attributeName, Class enumClass)
get Attribute Enum
return getAttributeEnum(node, attributeName, enumClass, null, false);
floatgetAttributeFloatValue(String attribute, NamedNodeMap namedNodeMap)
get Attribute Float Value
String value = getAttributeStringValue(attribute, namedNodeMap);
if (value != null) {
    return Float.parseFloat(value);
return 0.0f;
StringgetAttributeFromClosestAncestorOfAnyKind(Node node, String attributeName)
Climb through node's ancestors, looking for one with an attribute named attributeName, irrespective of the respective ancestorName, and return the attribute's value
Node parentNode;
while (node != null && (parentNode = node.getParentNode()) != null) {
    if (parentNode.hasAttributes()) {
        Element parentElement = (Element) parentNode;
        if (parentElement.hasAttribute(attributeName)) {
            return parentElement.getAttribute(attributeName);
    node = parentNode;
return "";
NodegetAttributeIfExists(Node node, String name)
get Attribute If Exists
NamedNodeMap nnm = node.getAttributes();
return nnm.getNamedItem(name);
AttrgetAttributeIgnoreCase(Element element, String attributeName)
Gets the DOM attribute of element with the given name.
NamedNodeMap nm = element.getAttributes();
for (int i = 0; i < nm.getLength(); i++) {
    Attr a = (Attr) nm.item(i);
    if (a.getName().equalsIgnoreCase(attributeName)) {
        return a;
return null;
...
StringgetAttributeIgnoreCase(Element element, String string)
get Attribute Ignore Case
NamedNodeMap map = element.getAttributes();
for (int i = 0; i < map.getLength(); i++) {
    Node attr = map.item(i);
    if (string.equalsIgnoreCase(attr.getNodeName())) {
        return attr.getNodeValue();
return null;
...