Java Utililty Methods XML Node Get by Name

List of utility methods to do XML Node Get by Name

Description

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

Method

NodeListgetElementsByTagName(Node node, String tagName)
get Elements By Tag Name
return null;
ListgetElementsByTagName(Node node, String tagname)
get Elements By Tag Name
ArrayList<Element> list = new ArrayList<Element>();
NodeList nl;
if (node.getNodeType() == Node.DOCUMENT_NODE) {
    nl = ((Document) node).getElementsByTagName(tagname);
} else if (node.getNodeType() == Node.ELEMENT_NODE) {
    nl = ((Element) node).getElementsByTagName(tagname);
} else {
    return null;
...
ArrayListgetElementsByTagName(Node node, String tagName)
get Elements By Tag Name
ArrayList<Element> elements = new ArrayList<Element>();
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
    Node n = node.getChildNodes().item(i);
    if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
        elements.add((Element) n);
return elements;
...
ListgetElementsByTagName(Node root, String tagName)
get Elements By Tag Name
List<Node> nodeList = new ArrayList<>();
if (root != null && root.getChildNodes().getLength() > 0) {
    int childCount = root.getChildNodes().getLength();
    for (int i = 0; i < childCount; i++) {
        Node node = root.getChildNodes().item(i);
        if (tagName.equals(node.getNodeName())) {
            nodeList.add(node);
return nodeList;
ArrayListgetElementsByTagNameNS(Node element, String namespace, String name)
get Elements By Tag Name NS
ArrayList<Element> childElements = new ArrayList<Element>();
NodeList elements = element.getChildNodes();
if (elements != null) {
    for (int i = 0; i < elements.getLength(); i++) {
        if (elements.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Element currentElement = (Element) elements.item(i);
            if ((currentElement.getNamespaceURI() == null
                    || currentElement.getNamespaceURI().equals(namespace))
...
ListgetElementsByTagNames(Node node, String... tagName)
get Elements By Tag Names
List<Element> nds = new ArrayList<Element>();
if (tagName.length == 0)
    tagName = new String[] { "" };
if (node.getNodeType() == Node.ELEMENT_NODE) {
    Element doc = (Element) node;
    for (String elementName : tagName) {
        nds.addAll(toElementList(doc.getElementsByTagName(elementName)));
} else if (node instanceof Document) {
    Document doc = ((Document) node);
    for (String elementName : tagName) {
        nds.addAll(toElementList(doc.getElementsByTagName(elementName)));
} else if (node instanceof DocumentFragment) {
    Document doc = ((DocumentFragment) node).getOwnerDocument();
    for (String elementName : tagName) {
        nds.addAll(toElementList(doc.getElementsByTagName(elementName)));
} else {
    throw new IllegalArgumentException("a node who doesn't support getElementsByTagName operation.");
return nds;
StringgetElementTagName(final Node aNode)
Retrieves the TagName for the supplied Node if it is an Element, and null otherwise.
if (aNode != null && aNode.getNodeType() == Node.ELEMENT_NODE) {
    final Element theElement = (Element) aNode;
    return theElement.getTagName();
return null;