Java XML First Child Element getFirstChildElementInternal(final Node node, final boolean useNamespaces, final String namespaceURI, final String localName)

Here you can find the source of getFirstChildElementInternal(final Node node, final boolean useNamespaces, final String namespaceURI, final String localName)

Description

Helper method to get the first child Element of a Node that matches specified criteria.

License

Open Source License

Parameter

Parameter Description
node the Node to obtain the child from (must not be <code>null</code>)
useNamespaces passed to the #matches(Node,boolean,String,String) method
namespaceURI passed to the #matches(Node,boolean,String,String) method
localName passed to the #matches(Node,boolean,String,String) method

Return

the first child of the given that matches the criteria, or null if none match

Declaration

private static Element getFirstChildElementInternal(final Node node, final boolean useNamespaces,
        final String namespaceURI, final String localName) 

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 {
    /**/*www  .ja  v a  2s. c  om*/
     * 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. getFirstChildElementByName(final Element parent, final String name)
  2. getFirstChildElementByName(Node node, String name)
  3. getFirstChildElementByTagName(Element element, String name)
  4. getFirstChildElementByTagName(Node element, String tagName)
  5. getFirstChildElementCaseInsensitive(Element root, String elementName)
  6. getFirstChildElementNode(Node node)
  7. getFirstChildElementNS(Element elm, String tns, String localName)
  8. getFirstChildElementNS(Node parent, String uri)
  9. getFirstChildElementNS(Node parent, String[][] elemNames)