Android XML Element Child Get getChildByType(Element element, short nodeType)

Here you can find the source of getChildByType(Element element, short nodeType)

Description

Returns first of the element's child nodes that is of type nodeType.

License

LGPL

Parameter

Parameter Description
element the element whose child we need.
nodeType the type of the child we need.

Return

a child of the specified nodeType or null if none was found.

Declaration

public static Node getChildByType(Element element, short nodeType) 

Method Source Code

//package com.java2s;
/*//from w ww .j  a  v  a2  s  . c om
 * 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 first of the <tt>element</tt>'s child nodes that is of type
     * <tt>nodeType</tt>.
     * @param element the element whose child we need.
     * @param nodeType the type of the child we need.
     * @return a child of the specified <tt>nodeType</tt> or null if none
     * was found.
     */
    public static Node getChildByType(Element element, short nodeType) {
        if (element == null)
            return null;

        NodeList nodes = element.getChildNodes();
        if (nodes == null || nodes.getLength() < 1)
            return null;

        Node node;
        String data;
        for (int i = 0; i < nodes.getLength(); i++) {
            node = nodes.item(i);
            short type = node.getNodeType();
            if (type == nodeType) {
                if (type == Node.TEXT_NODE
                        || type == Node.CDATA_SECTION_NODE) {
                    data = ((Text) node).getData();
                    if (data == null || data.trim().length() < 1)
                        continue;
                }

                return node;
            }
        }

        return null;
    }
}

Related

  1. findChildElementWithAttribute(Element parent, String name, String attribute, String value)
  2. findChildren(Element parent, String tagName)
  3. findFirstChildElement(Element parent)
  4. firstChildWithName(Element element, String name)
  5. getChild(Element element, String name)
  6. getChildElementByName(Element parent, String name)
  7. getChildValue(Element element, String name)
  8. getFullTextChildrenFromElement(Element element)
  9. printChildElements(Element root, PrintStream out, boolean recurse, String prefix)