Java XML First Child Element getFirstChild(Element elem, String childTag)

Here you can find the source of getFirstChild(Element elem, String childTag)

Description

get First Child

License

Open Source License

Parameter

Parameter Description
elem a parameter
childTag a parameter

Declaration

public static Element getFirstChild(Element elem, String childTag) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**/*from   w w  w  .  java 2s. co  m*/
     * @param elem
     * @param childTag
     * @return
     */
    public static Element getFirstChild(Element elem, String childTag) {
        if (elem.hasChildNodes()) {
            NodeList list = elem.getElementsByTagName(childTag);
            int count = list.getLength();

            for (int i = 0; i < count; i++) {
                Node node = list.item(i);
                if (node.getParentNode() != elem)
                    continue;

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    return (Element) node;
                }
            }
        }

        return null;
    }
}

Related

  1. getDirectChildNode(Node node, String name)
  2. getDirectChildNodes(final Node parent, final String... nodeNames)
  3. getDirectChildNodes(Node node, String name)
  4. getFirstChild(Element e)
  5. getFirstChild(Element e, String nsUri, String local)
  6. getFirstChild(Element element)
  7. getFirstChild(Element element)
  8. getFirstChild(Element element)
  9. getFirstChild(Element element)