Java XML Child Node Get getChildElements(final Node node)

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

Description

Enumerates all of the child Element s of the specified Node .

License

Open Source License

Parameter

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

Return

the child s of the specified (never null)

Declaration

public static Element[] getChildElements(final Node node) 

Method Source Code


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

import java.util.ArrayList;
import java.util.List;

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

public class Main {
    /**/*  w w w .j a va  2  s.co  m*/
     * Enumerates all of the child {@link Element}s of the specified
     * {@link Node}.
     *
     * @param node
     *        the {@link Node} to enumerate child {@link Element}s of (must not
     *        be <code>null</code>)
     * @return the child {@link Element}s of the specified {@link Node} (never
     *         <code>null</code>)
     */
    public static Element[] getChildElements(final Node node) {
        return getChildElementsInternal(node, false, null, null);
    }

    /**
     * Enumerates all of the child {@link Element}s of the specified
     * {@link Node} that have the specified name.
     *
     * @param tagName
     *        the child element name to match on, <code>null</code> or
     *        <code>*</code> to return child elements with any local name
     * @param node
     *        the {@link Node} to enumerate child {@link Element}s of (must not
     *        be <code>null</code>)
     * @return the child {@link Element}s that matched the specified criteria
     *         (never <code>null</code>)
     */
    public static Element[] getChildElements(final Node node, final String tagName) {
        return getChildElementsInternal(node, false, null, tagName);
    }

    /**
     * Helper method to enumerate child {@link Element}s of a {@link Node} that
     * match specified criteria. No <code>null</code> checking is done of the
     * <code>node</code> argument.
     *
     * @param node
     *        the {@link Node} to enumerate (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 {@link Element} children of the given {@link Node} (never
     *         <code>null</code>)
     */
    private static Element[] getChildElementsInternal(final Node node, final boolean useNamespaces,
            final String namespaceURI, final String localName) {
        final NodeList children = node.getChildNodes();
        final List childElements = new ArrayList();

        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)) {
                    childElements.add(child);
                }
            }
        }

        return (Element[]) childElements.toArray(new Element[childElements.size()]);
    }

    /**
     * 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. getChildElements(final Node parentNode, final String... childNodeNames)
  2. getChildElements(Node element)
  3. getChildElements(Node node)
  4. getChildElements(Node node)