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

ElementgetFirstChildElementByName(Element root, String tagName)
get First Child Element By Name
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node child = nl.item(i);
    if (child.getNodeType() != Node.ELEMENT_NODE) {
        continue;
    if (tagName.equals(child.getNodeName())) {
        return (Element) child;
...
OptionalgetFirstChildElementByName(final Element parent, final String name)
Returns the first element in the parent that match the specified name.
return getChildElementsByName(parent, name).stream().findFirst();
ElementgetFirstChildElementByName(Node node, String name)
Get the first child Element of the supplied node that matches a given tag name.
NodeList children = node.getChildNodes();
int childCount = children.getLength();
for (int i = 0; i < childCount; i++) {
    Node child = children.item(i);
    if (child != null && child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName() != null
            && child.getNodeName().equals(name)) {
        return (Element) child;
return null;
ElementgetFirstChildElementByTagName(Element element, String name)
get First Child Element By Tag Name
boolean found = false;
Element result = null;
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength() && !found; i++) {
    if (nl.item(i) instanceof Element) {
        if (name.equals(nl.item(i).getNodeName())) {
            result = (Element) nl.item(i);
            found = true;
...
ElementgetFirstChildElementByTagName(Node element, String tagName)
Gets the first element with the given tag name in the immediate child elements.
NodeList list = element.getChildNodes();
int size = list.getLength();
if (size > 0) {
    for (int i = 0; i < size; i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            if (e.getTagName().equals(tagName)) {
...
ElementgetFirstChildElementCaseInsensitive(Element root, String elementName)
get First Child Element Case Insensitive
NodeList nl = root.getChildNodes();
if (nl.getLength() > 1) {
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && (node.getNodeName().equalsIgnoreCase(elementName))) {
            return (Element) node;
return null;
ElementgetFirstChildElementInternal(final Node node, final boolean useNamespaces, final String namespaceURI, final String localName)
Helper method to get the first child Element of a Node that matches specified criteria.
final NodeList children = node.getChildNodes();
final int length = children.getLength();
for (int i = 0; i < length; i++) {
    final Node child = children.item(i);
    if (Node.ELEMENT_NODE == child.getNodeType()) {
        if (matches(child, useNamespaces, namespaceURI, localName)) {
            return (Element) child;
return null;
NodegetFirstChildElementNode(Node node)
get First Child Element Node
if (node == null)
    return (null);
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (Node.ELEMENT_NODE == child.getNodeType())
        return (child);
return (null);
ElementgetFirstChildElementNS(Element elm, String tns, String localName)
get First Child Element NS
if (tns == null && localName == null)
    return getFirstChildElement(elm);
if (tns == null || tns.length() == 0)
    return getFirstChildElement(elm, localName);
NodeList nl = elm.getChildNodes();
for (int c = 0; c < nl.getLength(); c++) {
    Node node = nl.item(c);
    if (node.getNodeType() != Node.ELEMENT_NODE)
...
ElementgetFirstChildElementNS(Node parent, String uri)
get First Child Element NS
Node child = parent.getFirstChild();
while (child != null) {
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        String childURI = child.getNamespaceURI();
        if (childURI != null && childURI.equals(uri)) {
            return (Element) child;
    child = child.getNextSibling();
return null;