Java Utililty Methods XML First Child Element

List of utility methods to do XML First Child Element

Description

The list of methods to do XML First Child Element are organized into topic(s).

Method

ElementgetFirstChild(Element root, String name)
Returns the first node that is a direct child of root with the coresponding 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))
...
ElementgetFirstChild(Element tag, String childTagName)
get First Child
NodeList nodes = tag.getElementsByTagName(childTagName);
if (nodes == null || nodes.getLength() == 0) {
    return null;
return (Element) nodes.item(0);
ElementgetFirstChild(final Element el, final String name)
Retrieves the given DOM element's first child element with the specified name.
final NodeList nodes = el.getChildNodes();
final int len = nodes.getLength();
for (int i = 0; i < len; i++) {
    final Node node = nodes.item(i);
    if (!(node instanceof Element))
        continue;
    final Element e = (Element) node;
    if (name == null || e.getTagName().equals(name))
...
ElementgetFirstChild(final Element parent, final String childName)
Gets the first child element node with the specified tag name of a specified parent node.
final NodeList childrenList = parent.getChildNodes();
final int childrenCount = childrenList.getLength();
for (int i = 0; i < childrenCount; ++i) {
    final Node child = childrenList.item(i);
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        final Element element = (Element) child;
        if (childName.equals(element.getTagName())) {
            return element;
...
ElementgetFirstChild(final Element parentElem, final String childName)
Gets the first child element with the specified name.
NodeList childList = parentElem.getElementsByTagName(childName);
if (childList.getLength() > 0) {
    Node childNode = childList.item(0);
    if (childNode instanceof Element && childNode.getParentNode() == parentElem) {
        return (Element) childNode;
    } else {
        return null;
} else {
    return null;
NodegetFirstChild(Node node, String childName)
get First Child
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
    Node child = nodeList.item(i);
    if (child.getNodeName().equals(childName)) {
        return child;
return null;
...
ElementgetFirstChild(Node parent)
Gets the first (direct) child Element.
if (parent == null)
    return null;
Node node = parent.getFirstChild();
while (node != null) { 
    if (node.getNodeType() == Node.ELEMENT_NODE)
        return (Element) node; 
    node = node.getNextSibling();
return null; 
ElementgetFirstChildByName(Element parent, String name)
Return the first child element with a given name.
NodeList children = parent.getElementsByTagName(name);
if (children.getLength() == 0) {
    return null;
return (Element) children.item(0);
NodegetFirstChildByNodeName(Node parent, String nodeName)
get First Child By Node Name
for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling())
    if (n.getNodeName().equals(nodeName))
        return n;
return null;
ElementgetFirstChildByNS(Element node, String ns, String localName)
get First Child By NS
Element e = null;
NodeList nl = node.getElementsByTagNameNS(ns, localName);
if (nl != null) {
    e = (Element) nl.item(0);
return e;