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

ElementgetChildElement(Element parentElement, String name)
get Child Element
Node node = parentElement.getFirstChild();
do {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        if (name.equals(element.getTagName())) {
            return element;
} while ((node = node.getNextSibling()) != null);
return null;
ElementgetChildElement(Element parentElement, String tagName)
This method returns the child element with the particular name.
NodeList nodes = parentElement.getElementsByTagName(tagName);
if (nodes != null && nodes.getLength() != 0) {
    return (Element) nodes.item(0);
} else {
    return null;
ElementgetChildElement(Element root, String name)
getChildElement purpose.
try {
    return getChildElement(root, name, false);
} catch (SAXException e) {
    return null;
ElementgetChildElement(Element root, String name, boolean mandatory)
getChildElement purpose.
Node child = root.getFirstChild();
while (child != null) {
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        if (name.equals(child.getNodeName())) {
            return (Element) child;
    child = child.getNextSibling();
...
ElementgetChildElement(final Element element, final String namespace, final String tagName)
Obtains the first child element with the specified name inside the specified namespace.
final NodeList childNodes = element.getChildNodes();
final int numChildren = childNodes.getLength();
for (int i = 0; i < numChildren; i++) {
    final Node childNode = childNodes.item(i);
    if (childNode.getNodeType() != Node.ELEMENT_NODE) {
        continue;
    final Element childElement = (Element) childNode;
...
ElementgetChildElement(final Element parent, final String childName)
get Child Element
Element child = null;
if (parent != null) {
    NodeList children = parent.getElementsByTagName(childName);
    if (children.getLength() > 0) {
        child = (Element) children.item(0);
return child;
...
ElementgetChildElement(final Element parent, final String ns, final String localName)
Get the first child element matching the local name and namespace
List<Element> elements = getChildElements(parent);
for (Element element : elements) {
    if ((ns == null || ns.equals(element.getNamespaceURI())
            && (localName == null || localName.equals(element.getLocalName())))) {
        return element;
return null;
...
ElementgetChildElement(final Node parent, final String name)
Look for child node of given name.
return findElementByName(parent.getFirstChild(), name);
ElementgetChildElement(final Node parentNode, final String childNodeName)
Get the first child element with the specified name from the given parent node.
final NodeList candidates = parentNode.getChildNodes();
final int childCount = candidates.getLength();
for (int childIndex = 0; childIndex < childCount; childIndex++) {
    final Node singleChild = candidates.item(childIndex);
    if (singleChild instanceof Element && childNodeName.equals(((Element) singleChild).getTagName())) {
        return (Element) singleChild;
return null;
ElementgetChildElement(Node node)
get Child Element
NodeList nl = node.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node n = nl.item(i);
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) n;
return null;
...