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

StringextractFirstChildValue(final Node node)
Extract text value for nodes like text
final Node valueSubNode = node.getFirstChild();
if (valueSubNode == null) {
    return null;
return valueSubNode.getNodeValue();
ElementfirstByClass(NodeList childList, String className)
first By Class
for (int i = 0; i < childList.getLength(); i++) {
    Element childElement = (Element) childList.item(i);
    if (className.equals(childElement.getAttribute("class"))) {
        return childElement;
return null;
ElementfirstChild(Element element, String name)
Returns the first child element for the given name
NodeList nodes = element.getChildNodes();
if (nodes != null) {
    for (int i = 0, size = nodes.getLength(); i < size; i++) {
        Node item = nodes.item(i);
        if (item instanceof Element) {
            Element childElement = (Element) item;
            if (name.equals(childElement.getTagName())) {
                return childElement;
...
ElementfirstChild(Element element, String name)
Extracts from the given Element the first child having the given name.
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
    Node current = nodeList.item(i);
    if (current.getNodeType() == Node.ELEMENT_NODE && current.getNodeName().equals(name)) {
        return (org.w3c.dom.Element) current;
return null;
...
NodefirstChildByTagName(Node root, String tagName)
first Child By Tag Name
if (root != null && root.getChildNodes().getLength() > 0) {
    int childCount = root.getChildNodes().getLength();
    for (int i = 0; i < childCount; i++) {
        if (tagName.equals(root.getChildNodes().item(i).getNodeName())) {
            return root.getChildNodes().item(i);
return null;
ElementfirstChildElement(Element element, String childElementName)
Return the first child Element with the given name; if name is null returns the first element.
if (element == null)
    return null;
Node node = element.getFirstChild();
if (node != null) {
    do {
        if (node.getNodeType() == Node.ELEMENT_NODE
                && (childElementName == null || childElementName.equals(node.getNodeName()))) {
            Element childElement = (Element) node;
...
ElementfirstChildElement(Element parent)
Get the first child org.w3c.dom.Element Element of an org.w3c.dom.Element Element .
Node n = parent.getFirstChild();
if (n == null || n instanceof Element) {
    return (Element) n;
} else {
    return nextSiblingElement(n);
ElementfirstChildElement(Node node)
Returns true if this node has at least one child element
if (node != null) {
    NodeList nodes = node.getChildNodes();
    for (int i = 0, size = nodes.getLength(); i < size; i++) {
        Node item = nodes.item(i);
        if (item instanceof Element) {
            return (Element) item;
return null;
ElementfirstChildElement(Node node)
first Child Element
for (Node tempNode = node.getFirstChild(); tempNode != null; tempNode = tempNode.getNextSibling()) {
    if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) tempNode;
return null;
org.w3c.dom.NodefirstChildNodeWithName(org.w3c.dom.Node node, String name)
first Child Node With Name
org.w3c.dom.Node foundNode = null;
org.w3c.dom.NodeList childNodes = node.getChildNodes();
final int n = childNodes.getLength();
for (int i = 0; i < n; i++) {
    org.w3c.dom.Node childNode = childNodes.item(i);
    if (childNode.getNodeName().equals(name)) {
        foundNode = childNode;
        break;
...