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

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

Description

Returns the child element with the specified tagName for the specified parent element.

License

LGPL

Parameter

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

Exception

Parameter Description
NullPointerException if parent or tagName are null

Return

The child with the specified name or null if no such child was found.

Declaration

public static Element findChild(Element parent, String tagName) 

Method Source Code

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

import org.w3c.dom.*;

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

        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) node).getNodeName().equals(tagName))
                return (Element) node;
        }

        return null;
    }
}

Related

  1. findChildElement(Element parent, String name)
  2. findChildElementWithAttribute(Element parent, String name, String attribute, String value)
  3. findChildren(Element parent, String tagName)
  4. findFirstChildElement(Element parent)