Java Utililty Methods XML Child Get by Name

List of utility methods to do XML Child Get by Name

Description

The list of methods to do XML Child Get by Name are organized into topic(s).

Method

ElementgetChildByTagName(Element element, String tag)
get Child By Tag Name
NodeList children = element.getChildNodes();
Node child;
for (int index = 0; index < children.getLength(); index++) {
    child = children.item(index);
    if ((child instanceof Element) && ((Element) child).getTagName().equals(tag)) {
        return (Element) child;
return null;
ElementgetChildByTagName(Element element, String tagName)
get Child By Tag Name
Element result = null;
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    Node n = childNodes.item(i);
    if (!(n instanceof Element)) {
        continue;
    if (((Element) n).getTagName().equals(tagName)) {
...
ElementgetChildByTagName(Node parent, String tagname)
Iterate over the children of a given node and return the first node that has a specific name.
if (parent == null) {
    return null;
NodeList childList = parent.getChildNodes();
final int len = childList.getLength();
for (int i = 0; i < len; i++) {
    Node child = childList.item(i);
    if (child != null && child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals(tagname)) {
...
StringgetChildData(Element e, String tag)
get Child Data
NodeList children = e.getElementsByTagName(tag);
if (children == null || children.getLength() == 0)
    return null;
return getChildData((Element) children.item(0));
OptionalgetChildDouble(final Element parent, final String name)
Given a parent element, locate double value of a child node.
final Element child = getChildElement(parent, name);
if (child == null)
    return Optional.empty();
try {
    return Optional.of(Double.valueOf(getString(child)));
} catch (NumberFormatException ex) {
    throw new Exception("Expected double for <" + name + ">", ex);
doublegetChildDoubleByName(Node node, String nodeName, double errValue)
get Child Double By Name
String s = getChildStringByName(node, nodeName, "");
if (s.length() > 0)
    return Double.parseDouble(s);
return errValue;
ElementgetChildElemByTagName(Element parent, String tagName)
returns first child element with fitting tag name.
NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); ++i) {
    if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
        Element child = (Element) children.item(i);
        if (child.getTagName().equals(tagName))
            return child;
return null;
ElementgetChildElement(Element currentNode, String nsURI, String localName)
get Child Element
return (Element) getChildElementList(currentNode, nsURI, localName).item(0);
ElementgetChildElement(Element el, String tagName)
get Child Element
if (el == null)
    return null;
NodeList list = el.getElementsByTagName(tagName);
if (list != null && list.getLength() > 0) {
    return (Element) list.item(0);
return null;
ElementgetChildElement(Element el, String tagName)
Gets the child element.
if (el == null) {
    return null;
NodeList list = el.getElementsByTagName(tagName);
if (list != null && list.getLength() > 0) {
    return (Element) list.item(0);
return null;
...