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

Element[]getChildElements(Element parent, String nsUri, String localPart)
Gets the child elements of the given name.
ArrayList a = new ArrayList();
NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node item = children.item(i);
    if (!(item instanceof Element))
        continue;
    if (nsUri.equals(item.getNamespaceURI()) && localPart.equals(item.getLocalName()))
        a.add(item);
...
ListgetChildElements(Element parent, String tagName)
Gets the immediately child elements list from the parent element.
NodeList nodes = parent.getElementsByTagName(tagName);
List<Element> elements = new ArrayList<>();
for (int i = 0; i < nodes.getLength(); i++) {
    Node node = nodes.item(i);
    if (node instanceof Element && node.getParentNode() == parent) {
        elements.add((Element) node);
return elements;
ListgetChildElements(Element parent, String tagName)
get Child Elements
NodeList nodes = parent.getElementsByTagName(tagName);
List<Element> elements = new ArrayList();
for (int i = 0; i < nodes.getLength(); i++) {
    Node node = nodes.item(i);
    if (((node instanceof Element)) && (node.getParentNode() == parent)) {
        elements.add((Element) node);
return elements;
ListgetChildElements(Element parent, String tagName)
Gets all child Element s of the given parent Element having the given tag name.
NodeList children = null;
if (tagName == null) {
    children = parent.getChildNodes();
} else {
    children = parent.getElementsByTagName(tagName);
List<Element> result = new ArrayList<Element>(children.getLength());
for (int i = 0; i < children.getLength(); i++) {
...
ListgetChildElements(Element parent, String tagName)
get Child Elements
if (null == parent)
    return null;
NodeList nodes = parent.getElementsByTagName(tagName);
List<Element> elements = new ArrayList<Element>();
for (int i = 0; i < nodes.getLength(); i++) {
    Node node = nodes.item(i);
    if (node.getParentNode() == parent)
        elements.add((Element) node);
...
ListgetChildElementsByName(Element element, String localName)
get Child Elements By Name
List<Element> elements = new ArrayList<Element>();
for (Element e : getChildElements(element)) {
    if (e.getLocalName() == null && localName.equals(e.getNodeName())) {
        elements.add(e);
    } else if (localName.equals(e.getLocalName())) {
        elements.add(e);
return elements;
ListgetChildElementsByName(Element parent, String elemName)
get Child Elements By Name
List<Element> result = new ArrayList<Element>();
NodeList nl = parent.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
        Element elem = (Element) nl.item(i);
        if (elem.getNodeName().equals(elemName))
            result.add(elem);
return result;
ListgetChildElementsByName(Element parent, String name)

Returns a list of child elements with the given name.

List elements = new ArrayList();
NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node node = children.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        if (element.getTagName().equals(name)) {
            elements.add(element);
...
ListgetChildElementsByName(Element parent, String name)
get Child Elements By Name
ArrayList<Element> array = new ArrayList<Element>();
NodeList list = parent.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    Node node = list.item(i);
    if (node.getNodeName().equals(name) && node instanceof Element) {
        array.add((Element) node);
return array;
ListgetChildElementsByName(Element parent, String tagName)
Get all child elements of a certain tag name.
List<Element> elementList = new LinkedList<Element>();
NodeList children = parent.getChildNodes();
for (int c = 0; c < children.getLength(); c++) {
    if (children.item(c).getNodeType() == Node.ELEMENT_NODE
            && children.item(c).getNodeName().equals(tagName)) {
        elementList.add((Element) children.item(c));
return elementList;