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

StringgetFirstChildTextValue(Element parent, String childName)
get First Child Text Value
NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (child instanceof Element) {
        Element elem = (Element) child;
        if (childName.equals(elem.getNodeName())) {
            return getOrCreateFirstTextChild(elem).getTextContent();
return null;
ElementgetFirstChildWithTagName(Element parent, String tagName)
get First Child With Tag Name
final NodeList elements = parent.getElementsByTagName(tagName);
return (elements.getLength() > 0) ? (Element) (elements.item(0)) : null;
ElementgetFirstChildWithTagName(Node data, String tagName)
The function getFirstChildWithTagName() returns the first child node from a node, identified by its node name.
for (Node element = data.getFirstChild(); element != null; element = element.getNextSibling()) {
    if (!(element instanceof Element)) {
        continue;
    if (element.getNodeName().equals(tagName)) {
        return (Element) element;
throw new NoSuchElementException(tagName);
ElementgetFirstElement(Element element, String childName)
get First Element
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (child instanceof Element && ((Element) child).getTagName().equals(childName)) {
        return (Element) child;
return null;
...
ElementgetFirstElementChild(Element aElement)
Gets the first DOM Element Node of the aElement DOM Element.
NodeList children = aElement.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
        return (Element) children.item(i);
return null;
ElementgetFirstElementChild(Element element)
get First Element Child
Node child = element.getFirstChild();
while (child != null) {
    if (child.getNodeType() == Node.ELEMENT_NODE)
        return (Element) child;
    child = child.getNextSibling();
return null;
ElementgetFirstElementChild(Element elemNode)
Returns the first element node within the children of the specified element.
NodeList nl = elemNode.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node cand = nl.item(i);
    if (cand.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) cand;
return null;
...
ElementgetFirstElementChild(Element elemNode)
Returns the first element node within the children of the specified element.
NodeList nl = elemNode.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node cand = nl.item(i);
    if (cand.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) cand;
return null;
...
ElementgetFirstElementChild(Element parent)
Finds the first element child, or null if not found.
for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
    if (n.getNodeType() == Node.ELEMENT_NODE)
        return (Element) n;
return null;
ElementGetFirstElementChild(Element tag)
Get First Element Child
Node n = tag.getFirstChild();
while (n.getNodeType() != Node.ELEMENT_NODE) {
    n = n.getNextSibling();
Element e = (Element) n;
return e;