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

ListgetChildElementsByTagName(Element parentNode, String tagName)
return all the child Elements under given Element parentNode and with the same tag name of tagName ; it's not deep search, so only the first generation children list is scanned
Note: the given tagName is just local name; namespace is not support right now.
if (parentNode == null) {
    return null;
if (tagName == null || tagName.trim().length() == 0) {
    return getChildElements(parentNode);
List<Element> resultList = new ArrayList<Element>();
NodeList childNodes = parentNode.getChildNodes();
...
ListgetChildElementsByTagName(final Element element, final String tagName)
get Child Elements By Tag Name
final List<Element> matches = new ArrayList<>();
final NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
    final Node node = nodeList.item(i);
    if (!(node instanceof Element)) {
        continue;
    final Element child = (Element) nodeList.item(i);
...
ListgetChildElementsByTagName(final Element parentElem, final String childName)
get Child Elements By Tag Name
NodeList childList = parentElem.getElementsByTagName(childName);
if (childList.getLength() > 0) {
    List<Element> childElements = new LinkedList<Element>();
    for (int i = 0; i < childList.getLength(); i++) {
        Node curr = childList.item(i);
        if (curr instanceof Element && curr.getParentNode() == parentElem) {
            childElements.add((Element) curr);
    return childElements;
} else {
    return Collections.emptyList();
ListgetChildElementsByTagName(Node element, String tagName)
get Child Elements By Tag Name
List<Node> list = new ArrayList<Node>();
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (child.getNodeName().equals(tagName)) {
        list.add(child);
return list;
java.util.ListgetChildElementsByTagName(org.w3c.dom.Element xmlParent, String tagName)
get Child Elements By Tag Name
java.util.List<org.w3c.dom.Element> rv = new java.util.LinkedList<org.w3c.dom.Element>();
org.w3c.dom.NodeList nodeList = xmlParent.getChildNodes();
final int N = nodeList.getLength();
for (int i = 0; i < N; i++) {
    org.w3c.dom.Node node = nodeList.item(i);
    if (node instanceof org.w3c.dom.Element) {
        org.w3c.dom.Element element = (org.w3c.dom.Element) node;
        if (tagName.equals(element.getTagName())) {
...
Element[]getChildElementsByTagName(String name, Element parent)
Return an array of direct child elements that have the given element tag name.
List<Element> elements = new ArrayList<Element>();
if (parent != null) {
    NodeList children = parent.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);
            if (node instanceof Element) {
                Element e = (Element) node;
...