Java Utililty Methods XML Node Find

List of utility methods to do XML Node Find

Description

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

Method

NodefindElementByTag(Set nodes, String tag)
find Element By Tag
Node result = null;
for (Node child : nodes) {
    if (tag.equals(child.getNodeName())) {
        result = child;
        break;
return result;
...
ElementfindElementNodeByName(Node node, String name)
find Element Node By Name
if (node == null) {
    return null;
NodeList nodeList = node.getChildNodes();
if (nodeList == null || nodeList.getLength() == 0) {
    return null;
for (int i = 0; i < nodeList.getLength(); i++) {
...
ListfindElements(Node fromnode, String name)
Simple DOM elements finder.
NodeList nodelist = fromnode.getChildNodes();
List<Element> list = new ArrayList<Element>();
for (int i = 0; i < nodelist.getLength(); i++) {
    Node node = nodelist.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        if (name.equals(node.getNodeName())) {
            list.add((Element) node);
        list.addAll(findElements(node, name));
return list;
voidfindElementsByName(Node node, List elements, String name)
find elements by name.
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (name.equalsIgnoreCase((child.getNodeName()))) {
        elements.add(child);
    } else {
        findElementsByName(child, elements, name);
NodefindNode(Document source, Element n)
Find a node in a DOM.
String name = n.getTagName();
NodeList list = source.getElementsByTagName(name);
int len = list.getLength();
if (len == 0)
    return null;
Element sn;
for (int i = 0; i < len; i++) {
    sn = (Element) list.item(i);
...
NodefindNode(Node aNode, String aName)
------------------------------------------------------------ Finds the first node of a given type
String name = aNode.getNodeName() != null ? aNode.getNodeName().trim() : "";
if (aName.equalsIgnoreCase(name)) {
    return aNode;
NodeList list = aNode.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    Node child = list.item(i);
    if (child != null) {
...
NodefindNode(Node node, String nodeName)
Find node by name recursively.
if (nodeName.equals(node.getNodeName()))
    return node;
org.w3c.dom.NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node ret = findNode(children.item(i), nodeName);
    if (ret != null)
        return ret;
return null;
NodefindNode(Node rootNode, String nodeName)
find Node
if (rootNode.getLocalName().equalsIgnoreCase(nodeName))
    return rootNode;
NodeList childrenList = rootNode.getChildNodes();
if (childrenList.getLength() > 0) {
    for (int i = 0; i < childrenList.getLength(); i++) {
        Node node = childrenList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Node findElement = findNode(node, nodeName);
...
NodefindNodeByAttribute(Document document, String tagName, String attributeName, String attributeValue)
find Node By Attribute
Node foundNode = null;
NodeList nodes = document.getElementsByTagName(tagName);
for (int i = 0; i < nodes.getLength(); i++) {
    Node node = nodes.item(i);
    for (int j = 0; j < node.getAttributes().getLength(); j++) {
        Node attribute = node.getAttributes().item(j);
        if (attribute.getNodeName().equals(attributeName)
                && attribute.getNodeValue().equals(attributeValue)) {
...
NodefindNodeByName(Document doc, String pathExpression)
Searches for a node within a DOM document with a given node path expression.
final StringTokenizer tok = new StringTokenizer(pathExpression, ".");
final int numToks = tok.countTokens();
NodeList elements;
if (numToks == 1) {
    elements = doc.getElementsByTagNameNS("*", pathExpression);
    return elements.item(0);
String element = pathExpression.substring(pathExpression.lastIndexOf('.') + 1);
...