Java XML First Child Element getFirstChild(Element root, String name)

Here you can find the source of getFirstChild(Element root, String name)

Description

Returns the first node that is a direct child of root with the coresponding name.

License

Open Source License

Declaration

public static Element getFirstChild(Element root, String name) 

Method Source Code


//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**/* w w  w  .j  av a  2  s  .  com*/
     * Returns the first node that is a direct child of root with the coresponding
     * name.  Does not search children of children.
     */
    public static Element getFirstChild(Element root, String name) {
        NodeList nl = root.getChildNodes();
        int size = nl.getLength();
        for (int i = 0; i < size; i++) {
            Node node = nl.item(i);
            if (!(node instanceof Element))
                continue;
            Element ele = (Element) node;
            if (ele.getTagName().equals(name))
                return ele;
        }

        return null;
    }
}

Related

  1. getFirstChild(Element element, String tag)
  2. getFirstChild(Element parent, String childTagName)
  3. getFirstChild(Element parent, String name)
  4. getFirstChild(Element parent, String name)
  5. getFirstChild(Element root)
  6. getFirstChild(Element tag, String childTagName)
  7. getFirstChild(final Element el, final String name)
  8. getFirstChild(final Element parent, final String childName)
  9. getFirstChild(final Element parentElem, final String childName)