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 element)
Get the first child element from the input elment.
return findNextSibling(element.getFirstChild());
ElementgetFirstChild(Element element)
Get the first child element of a document element.
if (element == null)
    return null;
NodeList list = element.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    if (list.item(i) instanceof Element) {
        return (Element) list.item(i);
return null;
ElementgetFirstChild(Element element)
get First Child
NodeList children = element.getChildNodes();
Node child;
for (int index = 0; index < children.getLength(); index++) {
    child = children.item(index);
    if ((child instanceof Element)) {
        return (Element) child;
return null;
ElementgetFirstChild(Element element, String child)
get First Child
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;
ElementgetFirstChild(Element element, String namespaceUri, String localName)
Returns the first child element of an element that belongs to a certain namespace and has a certain local name or null if none exists.
Element[] children = getChildren(element, namespaceUri, localName);
if (children.length > 0) {
    return children[0];
return null;
ElementgetFirstChild(Element element, String tag)
get First Child
Element result = null;
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    Node node = childNodes.item(i);
    if (Node.ELEMENT_NODE == node.getNodeType() && node.getNodeName().equals(tag)) {
        result = (Element) node;
        break;
return result;
ElementgetFirstChild(Element parent, String childTagName)
get First Child
NodeList nodes = parent.getElementsByTagName(childTagName);
if (nodes.getLength() == 0)
    return null;
return (Element) nodes.item(0);
ElementgetFirstChild(Element parent, String name)
Get the first child element with the given name.
final String filter = (name != null && !name.equals("*")) ? name : null;
for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
    if (n.getNodeType() == Node.ELEMENT_NODE && (filter == null || n.getNodeName().equals(filter)))
        return (Element) n;
return null;
ElementgetFirstChild(Element parent, String name)
get First Child
NodeList nl = parent.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node n = nl.item(i);
    if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
        return (Element) n;
return null;
...
ElementgetFirstChild(Element root)
Get the first child element of the specified element, null if it has no child elements.
if (root == null)
    return null;
NodeList lst = root.getChildNodes();
final int n = lst.getLength();
for (int i = 0; i < n; i++) {
    Node node = lst.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE)
        return (Element) node;
...