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

ListgetFirstLevelChildElementsByTagName(Element parent, String elementName)
Get all the first level child elements with a given tag name.
NodeList nodeList = parent.getChildNodes();
ArrayList<Element> childList = new ArrayList<Element>(nodeList.getLength());
Node childNode = parent.getFirstChild();
while (childNode != null) {
    if (childNode.getNodeType() == Node.ELEMENT_NODE) {
        String localName = ((Element) childNode).getLocalName();
        if (localName.equals(elementName)) {
            childList.add((Element) childNode);
...
ListgetFirstLevelChildElementsByTagName(Element parent, String elementName)
get First Level Child Elements By Tag Name
NodeList nodeList = parent.getChildNodes();
ArrayList<Element> childList = new ArrayList<Element>(nodeList.getLength());
Node childNode = parent.getFirstChild();
while (childNode != null) {
    if (childNode.getNodeType() == Node.ELEMENT_NODE) {
        String localName = ((Element) childNode).getLocalName();
        if (localName.equals(elementName)) {
            childList.add((Element) childNode);
...
StringgetFirstMatchedValueByChildTagName(Node parent, String name)
get First Matched Value By Child Tag Name
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        if (child.getNodeName().indexOf(name) != -1) {
            return child.getTextContent();
        } else {
            String value = getFirstMatchedValueByChildTagName((Element) child, name);
            if (value != null) {
                return value;
...
ElementgetFirstMatchingDeepChildByTagName(final Element e, final String tagName)
get First Matching Deep Child By Tag Name
NodeList matchingElements = e.getElementsByTagName(tagName);
if (matchingElements != null) {
    return (Element) matchingElements.item(0);
} else {
    return null;
NodegetFirstNamedChild(Node n, String name)
Check if the given node has a child with the specified name Return the child of the specified name, or null if not found
NodeList children = n.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node currentChild = children.item(i);
    String localName = currentChild.getNodeName();
    if (localName != null && localName.equalsIgnoreCase(name)) {
        return currentChild;
return null;
ElementgetFirstNamedChild(Node node, String name)
Get the first child element with a specified name.
if (node == null)
    return null;
if (node instanceof Document)
    node = ((Document) node).getDocumentElement();
if (!(node instanceof Element))
    return null;
Node child = node.getFirstChild();
while (child != null) {
...
NodegetFirstNamedChildNode(Element element, String string)
get First Named Child Node
NodeList children = element.getChildNodes();
if (children != null && children.getLength() > 0) {
    for (int i = 0; i < children.getLength(); i++) {
        Node item = children.item(i);
        if (item.getNodeName().equals(string)) {
            return item;
return null;
NodegetFirstNamedChildNode(Node root, String nodeName)
get First Named Child Node
Node current = root.getFirstChild();
while (current != null) {
    if (current.getNodeName().equalsIgnoreCase(nodeName)) {
        return current;
    current = current.getNextSibling();
return null;
...
ElementgetFirstNontextChild(Node d)
get First Nontext Child
Node n = d.getFirstChild();
while ((n != null) && (n.getNodeType() != Node.ELEMENT_NODE)) {
    n = n.getNextSibling();
return (Element) n;
NodegetFirstNonTextChild(Node root)
Returns the first non-text child node of the given node.
Node resultNode = null;
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    if (!isTextNode(nl.item(i))) {
        resultNode = nl.item(i);
        break;
return resultNode;