Java XML First Child Element getFirstChildElement(final Node node)

Here you can find the source of getFirstChildElement(final Node node)

Description

Gets the first child Element of the specified Node , if any.

License

Open Source License

Parameter

Parameter Description
node the Node to get the first child Element s of (must not be <code>null</code>)

Return

the first child of the specified , or null if there were no child elements

Declaration

public static Element getFirstChildElement(final Node node) 

Method Source Code

//package com.java2s;
// Licensed under the MIT license. See License.txt in the repository root.

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**//from   w  ww  .  j av  a  2s .c o m
     * Gets the first child {@link Element} of the specified {@link Node}, if
     * any.
     *
     * @param node
     *        the {@link Node} to get the first child {@link Element}s of (must
     *        not be <code>null</code>)
     * @return the first child {@link Element} of the specified {@link Node}, or
     *         <code>null</code> if there were no child elements
     */
    public static Element getFirstChildElement(final Node node) {
        return getFirstChildElementInternal(node, false, null, null);
    }

    /**
     * Gets the first child {@link Element} of the specified {@link Node} that
     * has the specified name, if any.
     *
     * @param tagName
     *        the child element name to match on, <code>null</code> or
     *        <code>*</code> to match child elements with any local name
     * @param node
     *        the {@link Node} to get the first child {@link Element} of (must
     *        not be <code>null</code>)
     * @return the first child {@link Element} that matched the specified
     *         criteria, or <code>null</code> if there were no child elements
     *         that matched
     */
    public static Element getFirstChildElement(final Node node, final String tagName) {
        return getFirstChildElementInternal(node, false, null, tagName);
    }

    /**
     * Helper method to get the first child {@link Element} of a {@link Node}
     * that matches specified criteria. No <code>null</code> checking is done of
     * the <code>node</code> argument.
     *
     * @param node
     *        the {@link Node} to obtain the child from (must not be
     *        <code>null</code>)
     * @param useNamespaces
     *        passed to the {@link #matches(Node, boolean, String, String)}
     *        method
     * @param namespaceURI
     *        passed to the {@link #matches(Node, boolean, String, String)}
     *        method
     * @param localName
     *        passed to the {@link #matches(Node, boolean, String, String)}
     *        method
     * @return the first child {@link Element} of the given {@link Node} that
     *         matches the criteria, or <code>null</code> if none match
     */
    private static Element getFirstChildElementInternal(final Node node, final boolean useNamespaces,
            final String namespaceURI, final String localName) {
        final NodeList children = node.getChildNodes();

        final int length = children.getLength();
        for (int i = 0; i < length; i++) {
            final Node child = children.item(i);
            if (Node.ELEMENT_NODE == child.getNodeType()) {
                if (matches(child, useNamespaces, namespaceURI, localName)) {
                    return (Element) child;
                }
            }
        }

        return null;
    }

    /**
     * Tests whether the given {@link Node} matches the specified name. No
     * <code>null</code> checking is done of the <code>node</code> argument.
     *
     * @param node
     *        the {@link Node} to test (must not be <code>null</code>)
     * @param useNamespaces
     *        <code>true</code> for namespaces mode
     * @param namespaceURI
     *        the namespace URI to match, <code>null</code> to match no
     *        namespace, <code>*</code> to match any namespace (ignored if
     *        <code>useNamespaces</code> is <code>false</code>)
     * @param localName
     *        <code>null</code> or <code>*</code> to match any node, matches the
     *        node name if <code>useNamespaces</code> is <code>false</code>,
     *        matches the local name if <code>useNamespaces</code> is
     *        <code>true</code>
     * @return <code>true</code> if the {@link Node} matches the criteria
     */
    private static boolean matches(final Node node, final boolean useNamespaces, final String namespaceURI,
            final String localName) {
        if (useNamespaces) {
            if (namespaceURI == null && node.getNamespaceURI() != null) {
                return false;
            }

            if (namespaceURI != null && !namespaceURI.equals("*") && !namespaceURI.equals(node.getNamespaceURI())) //$NON-NLS-1$
            {
                return false;
            }
        }

        if (localName == null || localName.equals("*")) //$NON-NLS-1$
        {
            return true;
        }

        if (useNamespaces) {
            return localName.equals(node.getLocalName());
        }

        return localName.equals(node.getNodeName());
    }
}

Related

  1. getFirstChildElement(Element parent, String nsUri, String localPart)
  2. getFirstChildElement(Element parent, String tagName)
  3. getFirstChildElement(Element parent, String tagName)
  4. getFirstChildElement(Element parentElem)
  5. getFirstChildElement(Element root)
  6. getFirstChildElement(final Node parent, final String elemName)
  7. getFirstChildElement(Node n)
  8. getFirstChildElement(Node n, String ns, String localName)
  9. getFirstChildElement(Node node)