Java Utililty Methods XML Element Get by Name

List of utility methods to do XML Element Get by Name

Description

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

Method

ListgetElements(Element parent, String tagName)
get Elements
List<Element> elements = new ArrayList<Element>();
NodeList nodes = parent.getElementsByTagName(tagName);
for (int i = 0; i < nodes.getLength(); i++) {
    Node node = nodes.item(i);
    if (node instanceof Element) {
        elements.add(Element.class.cast(node));
return elements;
ListgetElements(Element parent, String tagName)
Gets the descendant 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) {
        elements.add((Element) node);
return elements;
ListgetElements(Element parent, String tagName)
get 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)) {
        elements.add((Element) node);
return elements;
Element[]getElements(Element parentElement, String nodeName)
get Elements
NodeList list = parentElement.getElementsByTagName(nodeName);
if (list == null) {
    return new Element[0];
Element[] eles = new Element[list.getLength()];
for (int i = 0; i < list.getLength(); i++) {
    Node node = list.item(i);
    eles[i] = (Element) node;
...
ListgetElements(final Element parent, final String name)
Get all the direct children elements of an element that have a specific tag name.
final LinkedList<Element> list = new LinkedList<Element>();
Node node = parent.getFirstChild();
while (node != null) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        final Element element = (Element) node;
        if (element.getTagName().equals(name)) {
            list.add(element);
    node = node.getNextSibling();
return list;
Element[]getElementsByName(Element parent, String name)
Get the node set from parent node by the specified name
ArrayList resList = new ArrayList();
NodeList nl = getNodeList(parent);
for (int i = 0; i < nl.getLength(); i++) {
    Node nd = nl.item(i);
    if (nd.getNodeName().equals(name)) {
        resList.add(nd);
Element[] res = new Element[resList.size()];
for (int i = 0; i < resList.size(); i++) {
    res[0] = (Element) resList.get(i);
return res;
Node[]getElementsByTagName(Document doc, String tagName)
get Elements By Tag Name
try {
    NodeList list = doc.getElementsByTagName(tagName);
    int length = list.getLength();
    if (length > 0) {
        Node[] array = new Node[length];
        for (int i = 0; i < length; i++) {
            array[i] = list.item(i);
        return array;
    return null;
} catch (Exception ex) {
    throw new RuntimeException(ex);
ListgetElementsByTagName(Element parent, String name, boolean localOnly)
get Elements By Tag Name
List<Element> ret = new ArrayList<Element>();
if (!localOnly) {
    NodeList elementList = parent.getElementsByTagName(name);
    for (int i = 0; i < elementList.getLength(); i++) {
        ret.add((Element) elementList.item(i));
} else {
    NodeList childList = parent.getChildNodes();
...
Element[]getElementsByTagName(Element parent, String tagName)
Returns an Element array of all first level descendant Elements with a given tag name, in the order in which they are encountered in the DOM.
NodeList allChildElements = parent.getElementsByTagName(tagName);
Vector vector = new Vector();
for (int i = 0; i < allChildElements.getLength(); i++) {
    Element aElement = (Element) allChildElements.item(i);
    if (aElement.getParentNode().equals(parent))
        vector.add(aElement);
Element[] filteredElements = new Element[vector.size()];
...
ListgetElementsByTagName(final Element parentElement, final String elementName)
Get the list of all the elements of a parent element.
if (elementName == null || parentElement == null) {
    return null;
final NodeList nStepsList = parentElement.getElementsByTagName(elementName);
if (nStepsList == null) {
    return null;
final List<Element> result = new ArrayList<>();
...