Android XML Element Child Get findChildren(Element parent, String tagName)

Here you can find the source of findChildren(Element parent, String tagName)

Description

Returns the children elements with the specified tagName for the specified parent element.

License

LGPL

Parameter

Parameter Description
parent The parent whose children we're looking for.
tagName the name of the child to find

Exception

Parameter Description
NullPointerException if parent or tagName are null

Return

List of the children with the specified name

Declaration

public static List<Element> findChildren(Element parent, String tagName) 

Method Source Code

//package com.java2s;
/*//from w  ww.ja  va  2s  . c om
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

import java.util.*;

import org.w3c.dom.*;

public class Main {
    /**
     * Returns the children elements with the specified tagName for the
     * specified parent element.
     *
     * @param parent The parent whose children we're looking for.
     * @param tagName the name of the child to find
     * @return List of the children with the specified name
     * @throws NullPointerException if parent or tagName are null
     */
    public static List<Element> findChildren(Element parent, String tagName) {
        if (parent == null || tagName == null)
            throw new NullPointerException("Parent or tagname were null! "
                    + "parent = " + parent + "; tagName = " + tagName);

        List<Element> result = new ArrayList<Element>();
        NodeList nodes = parent.getChildNodes();
        Node node;
        int len = nodes.getLength();
        for (int i = 0; i < len; i++) {
            node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                if (element.getNodeName().equals(tagName))
                    result.add(element);
            }
        }

        return result;
    }
}

Related

  1. findChild(Element parent, String tagName)
  2. findChildElement(Element parent, String name)
  3. findChildElementWithAttribute(Element parent, String name, String attribute, String value)
  4. findFirstChildElement(Element parent)
  5. firstChildWithName(Element element, String name)
  6. getChild(Element element, String name)
  7. getChildByType(Element element, short nodeType)