Java XML Attribute Get getCascadeValue(final Element elem, final String attrName)

Here you can find the source of getCascadeValue(final Element elem, final String attrName)

Description

Get cascaded attribute value.

License

Apache License

Parameter

Parameter Description
elem attribute parent element
attrName attribute name

Return

attribute value, null if not set

Declaration

public static String getCascadeValue(final Element elem, final String attrName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.*;

public class Main {
    /**/* w  w w . ja v a  2s. c o m*/
     * Get cascaded attribute value.
     *
     * @param elem attribute parent element
     * @param attrName attribute name
     * @return attribute value, {@code null} if not set
     */
    public static String getCascadeValue(final Element elem, final String attrName) {
        Element current = elem;
        while (current != null) {
            final Attr attr = current.getAttributeNode(attrName);
            if (attr != null) {
                return attr.getValue();
            }
            final Node parent = current.getParentNode();
            if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
                current = (Element) parent;
            } else {
                break;
            }
        }
        return null;
    }

    /**
     * Get attribute value.
     *
     * @param elem attribute parent element
     * @param attrName attribute name
     * @return attribute value, {@code null} if not set
     */
    public static String getValue(final Element elem, final String attrName) {
        final Attr attr = elem.getAttributeNode(attrName);
        if (attr != null && !attr.getValue().isEmpty()) {
            return attr.getValue();
        }
        return null;
    }
}

Related

  1. getBooleanAttributeByName(Node content, String attributeName, boolean defaultTrue)
  2. getBooleanAttributeOption(final Element configuration, final String option, boolean defaultValue)
  3. getBooleanAttributeOptional(Node node, String attributeName, Boolean valueIfEmpty)
  4. getBooleanAttributeRequired(Node node, String attributeName)
  5. getBooleanAttributeValue(Node node, String attribute)
  6. getClosestAncestorWithAttribute(Node node, String ancestorName, String attributeName)
  7. getClosestAncestorWithAttribute(Node node, String ancestorName, String attributeName)
  8. getCurrentLevelAttributeTextValue(Element ele, String attribute)
  9. getDirectAttribute(Node node, String name)