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(Node node, String childName)
get Child Element
NodeList childNodes = node.getChildNodes();
int length = childNodes.getLength();
for (int i = 0; i < length; i++) {
    Node item = childNodes.item(i);
    if (item instanceof Element && item.getNodeName().equals(childName)) {
        return (Element) item;
return null;
ElementgetChildElement(Node parent, String childLocalName, String childNamespaceURI)
Returns the first child element that matches the given local name and namespace.
if (parent != null) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE && childLocalName.equals(child.getLocalName())
                && childNamespaceURI.equals(child.getNamespaceURI())) {
            return (Element) child;
return null;
ElementgetChildElement(Node parent, String childName)
Gets a named child node.
return (Element) getChildNode(parent, childName, Node.ELEMENT_NODE);
NodegetChildElement(Node parent, String elementName)
get Child Element
NodeList childNodes = parent.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    Node childNode = childNodes.item(i);
    if (elementName.equals(childNode.getNodeName()))
        return childNode;
return null;
ElementgetChildElement(Node parent, String name)
Returns a node child with the specified tag name of the specified parent node, or null if no such child node is found.
NodeList childList = parent.getChildNodes();
for (int i = 0; i < childList.getLength(); i++) {
    Node node = childList.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE
            && (name.equals(node.getLocalName()) || name.equals(node.getNodeName()))) {
        return (Element) node;
return null;
ElementgetChildElement(Node start, String name)
__UNDOCUMENTED__
NodeList nl = null;
if (start.getNodeType() == Node.DOCUMENT_NODE) {
    nl = ((Document) start).getDocumentElement().getChildNodes();
} else {
    nl = start.getChildNodes();
int len = nl.getLength();
Node n = null;
...
ElementgetChildElement(NodeList childs, Node parent)
Method provides getting Element objects out of a NodeList.
Node childNode;
String parentName;
parentName = parent.getNodeName();
for (int i = 0; i < childs.getLength(); i++) {
    childNode = childs.item(i);
    if ((childNode instanceof Element) && (childNode.getParentNode().getNodeName().equals(parentName))) {
        return (Element) childNode;
return null;
ElementgetChildElement(String name, Element el)
Gets the given element's first child DOM element with the specified name.
if (name == null || el == null)
    return null;
NodeList list = el.getChildNodes();
int size = list.getLength();
for (int i = 0; i < size; i++) {
    Node node = list.item(i);
    if (!(node instanceof Element))
        continue;
...
ElementgetChildElement(String name, Element elem)
Get a child element with a specific name.
NodeList l = elem.getChildNodes();
for (int i = 0; i < l.getLength(); i++) {
    Node n = l.item(i);
    if (n instanceof Element && n.getNodeName().equals(name))
        return (Element) n;
return null;
ListgetChildElements(Element element, String childrenName)
Given an XML element, this method returns nested elements which are direct children of the given element - but only those that have the element-name "childrenName" (the second parameter).
List<Element> ret = new LinkedList<Element>();
NodeList nodeList = element.getChildNodes();
for (int index = 0; index < nodeList.getLength(); ++index) {
    Node node = nodeList.item(index);
    if (node instanceof Element) {
        Element elementOfNode = (Element) node;
        if (elementOfNode.getTagName().equals(childrenName)) {
            ret.add(elementOfNode);
...