Java XML Node Value Check isNamedElement(final Node aNode)

Here you can find the source of isNamedElement(final Node aNode)

Description

Checks if the supplied DOM Node is a DOM Element having a defined "name" attribute.

License

Apache License

Parameter

Parameter Description
aNode A DOM Node.

Return

true if the supplied aNode is an Elemnet having a defined "name" attribute.

Declaration

public static boolean isNamedElement(final Node aNode) 

Method Source Code

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

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    private static final String NAME_ATTRIBUTE = "name";

    /**/* w w w  .j  a v  a 2s  .  c  o  m*/
     * Checks if the supplied DOM Node is a DOM Element having a defined "name" attribute.
     *
     * @param aNode A DOM Node.
     * @return {@code true} if the supplied aNode is an Elemnet having a defined "name" attribute.
     */
    public static boolean isNamedElement(final Node aNode) {

        final boolean isElementNode = aNode != null && aNode.getNodeType() == Node.ELEMENT_NODE;

        return isElementNode && getNamedAttribute(aNode, NAME_ATTRIBUTE) != null
                && !getNamedAttribute(aNode, NAME_ATTRIBUTE).isEmpty();
    }

    private static String getNamedAttribute(final Node aNode, final String attributeName) {

        // Fail fast
        if (aNode == null) {
            return null;
        }

        final NamedNodeMap attributes = aNode.getAttributes();
        if (attributes != null) {

            final Node nameNode = attributes.getNamedItem(attributeName);
            if (nameNode != null) {
                return nameNode.getNodeValue().trim();
            }
        }

        // Not found.
        return null;
    }
}

Related

  1. isInlineNode(@Nullable final Node aNode)
  2. isInsertNode(Node n)
  3. isJunk(Node node)
  4. isLeaf(Node node)
  5. isMixed(org.w3c.dom.Node node)
  6. isNode(Object value, String nodeName)
  7. isNodeAfter(Node node1, Node node2)
  8. isNodeNameEquals(Node node, String desiredName)
  9. isNodeTheSame(Node node1, Node node2)