Java XML First Child Element getFirstChild(Element element, String child)

Here you can find the source of getFirstChild(Element element, String child)

Description

get First Child

License

Open Source License

Parameter

Parameter Description
child a parameter

Exception

Parameter Description
Exception if the child is present multiple times

Return

the single child element or null

Declaration

public static Element getFirstChild(Element element, String child) throws Exception 

Method Source Code

//package com.java2s;

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

public class Main {
    /**//ww  w  .  ja va  2s .c o m
     * @param child
     * @return the single child element or null
     * @throws Exception if the child is present multiple times
     */
    public static Element getFirstChild(Element element, String child) throws Exception {
        NodeList nodes = element.getChildNodes();
        Element ret = null;
        for (int i = 0; i < nodes.getLength(); i++) {
            Node childNode = nodes.item(i);
            if (childNode.getNodeName().equalsIgnoreCase(child) && childNode.getNodeType() == Node.ELEMENT_NODE) {
                ret = (Element) childNode;
                return ret;
            }
        }
        return null;
    }
}

Related

  1. getFirstChild(Element elem, String childTag)
  2. getFirstChild(Element element)
  3. getFirstChild(Element element)
  4. getFirstChild(Element element)
  5. getFirstChild(Element element)
  6. getFirstChild(Element element, String namespaceUri, String localName)
  7. getFirstChild(Element element, String tag)
  8. getFirstChild(Element parent, String childTagName)
  9. getFirstChild(Element parent, String name)