Java XML Child Get getChildElements(final Element element, final String localName)

Here you can find the source of getChildElements(final Element element, final String localName)

Description

Retrieves all child Element s of a given local name from a given parent Element as List .

License

Apache License

Parameter

Parameter Description
element the parent Element
localName the local name of the child Element s to retrieve

Return

the respective of child s

Declaration

public static List<Element> getChildElements(final Element element, final String localName) 

Method Source Code

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

import java.util.ArrayList;

import java.util.List;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /**/*from  w w  w  .j a va  2  s .  c  om*/
     * Retrieves all child {@link Element}s from a given parent {@link Element}
     * as {@link List}.
     * 
     * @param element
     *           the parent {@link Element}
     * @return the {@link List} of child {@link Element}s
     */
    public static List<Element> getChildElements(final Element element) {
        final List<Element> result = new ArrayList<Element>();

        for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (!(child instanceof Element)) {
                continue;
            }

            result.add((Element) child);
        }

        return result;
    }

    /**
     * Retrieves all child {@link Element}s of a given namespace URI and local
     * name from a given parent {@link Element} as {@link List}.
     * 
     * @param element
     *           the parent {@link Element}
     * @param namespaceURI
     *           the namespace URI of the child {@link Element}s to retrieve
     * @param localName
     *           the local name of the child {@link Element}s to retrieve
     * @return the respective {@link List} of child {@link Element}s
     */
    public static List<Element> getChildElements(final Element element, final String namespaceURI,
            final String localName) {
        final List<Element> result = new ArrayList<Element>();

        for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (!(child instanceof Element)) {
                continue;
            }

            if (namespaceURI != null && !namespaceURI.equals(child.getNamespaceURI())) {
                continue;
            }

            if (!localName.equals(child.getLocalName())) {
                continue;
            }

            result.add((Element) child);
        }

        return result;
    }

    /**
     * Retrieves all child {@link Element}s of a given local name from a given
     * parent {@link Element} as {@link List}.
     * 
     * @param element
     *           the parent {@link Element}
     * @param localName
     *           the local name of the child {@link Element}s to retrieve
     * @return the respective {@link List} of child {@link Element}s
     */
    public static List<Element> getChildElements(final Element element, final String localName) {
        final List<Element> result = new ArrayList<Element>();

        for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (!(child instanceof Element)) {
                continue;
            }

            if (!localName.equals(child.getNodeName())) {
                continue;
            }

            result.add((Element) child);
        }

        return result;
    }

    static String getLocalName(final Node node) {
        final String result = node.getLocalName();
        return result != null ? result : node.getNodeName();
    }
}

Related

  1. getChildElements(Element parentElement, String childElementTagName)
  2. getChildElements(Element parentNode)
  3. getChildElements(Element root, String name, boolean mandatory)
  4. getChildElements(final Element elem)
  5. getChildElements(final Element element)
  6. getChildElements(final Element element, final String namespace, final String tagName)
  7. getChildElements(final Element elm)