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

NodegetFirstChildNodeWithName(String nodeName, Node parentNode)
get First Child Node With Name
parentNode.normalize();
NodeList childNodes = parentNode.getChildNodes();
Node resultNode = null;
for (int i = 0; i < childNodes.getLength(); i++) {
    Node childElement = childNodes.item(i);
    if (childElement.getNodeName().equalsIgnoreCase(nodeName)) {
        resultNode = childElement;
return resultNode;
ElementgetFirstChildOfTagName(Element elem, String name)
get First Child Of Tag Name
NodeList nodeList = elem.getChildNodes();
if (nodeList == null) {
    return null;
for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);
    if (node instanceof Element) {
        elem = (Element) node;
...
ElementgetFirstChildOfType(final Element elParent, final String childTag)
Returns the first child element with the specified tag.
for (Node childNode = elParent.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
    if (childNode.getNodeType() == Node.ELEMENT_NODE) {
        final Element elChild = (Element) childNode;
        if (elChild.getTagName().equals(childTag)) {
            return elChild;
return null;
ElementgetFirstChildTag(Element el, String tag)
Method getFirstChildTag.
NodeList nl;
int len;
nl = el.getChildNodes();
len = nl.getLength();
for (int i = 0; i < len; ++i) {
    Node n = nl.item(i);
    if (n instanceof Element) {
        Element elem = (Element) n;
...
StringgetFirstChildText(@Nullable final Node aStartNode)
Get the content of the first Text child element of the passed element.
if (aStartNode != null) {
    final NodeList aNodeList = aStartNode.getChildNodes();
    final int nLen = aNodeList.getLength();
    for (int i = 0; i < nLen; ++i) {
        final Node aNode = aNodeList.item(i);
        if (aNode instanceof Text) {
            final Text aText = (Text) aNode;
            if (!aText.isElementContentWhitespace())
...
NodegetFirstChildText(Node aNode)
get First Child Text
if (aNode == null) {
    return null;
Node wNode = aNode.getFirstChild();
if (wNode == null || wNode.getNodeType() == Node.TEXT_NODE) {
    return wNode;
} else {
    return getFirstSiblingElmt(wNode);
...
TextgetFirstChildText(Node parent)
Get the first Text child of the given parent.
return getFirstChildImplementing(parent, Text.class);
StringgetFirstChildTextByTagName(Element element, String tagName)
get First Child Text By Tag Name
Element e = getFirstChildElementByTagName(element, tagName);
return e == null ? null : e.getTextContent().trim();
StringgetFirstChildTextContent(Node node)
get First Child Text Content
String content = "";
if (node != null) {
    try {
        Node n = node.getFirstChild();
        if (n != null) {
            content = n.getTextContent();
    } catch (Exception e) {
...
StringgetFirstChildTextNodeValue(Node node)
Returns value of first available child text node or null if not found.
NodeList children = node.getChildNodes();
int len = children.getLength();
for (int i = 0; i < len; i++) {
    Node n = children.item(i);
    if (n.getNodeType() == Node.TEXT_NODE) {
        return n.getNodeValue();
return null;