Java XML Node Find findAllNodesRecursive(Node n, String name)

Here you can find the source of findAllNodesRecursive(Node n, String name)

Description

find All Nodes Recursive

License

Open Source License

Parameter

Parameter Description
n a root node to search beneath
name a node name to look for

Return

all instances of that node name in the tree beneath the root

Declaration

public static List<Node> findAllNodesRecursive(Node n, String name) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.List;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /**/*from   www .  j  a  v a 2s  . c o  m*/
     * @param n a root node to search beneath
     * @param name a node name to look for
     * @return all instances of that node name in the tree beneath the root
     */
    public static List<Node> findAllNodesRecursive(Node n, String name) {
        List<Node> nodes = new ArrayList<Node>();
        findRecursive(n, name, nodes, false);
        return nodes;
    }

    private static void findRecursive(Node parent, String name, List<Node> nodes, boolean onlyOne) {
        String nn = parent.getNodeName();
        int off = nn.indexOf(':');
        if (off >= 0)
            nn = nn.substring(off + 1);
        if (nn.equals(name)) {
            nodes.add(parent);
            if (onlyOne)
                return;
        }
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
            findRecursive(child, name, nodes, onlyOne);
            if (onlyOne && (nodes.size() > 0))
                return;
        }
    }

    /**
     * @param n1 first Node to test
     * @param n2 second Node to test
     * @return true if a deep compare show the same children and attributes in the same order
     */
    public static boolean equals(Node n1, Node n2) {
        // compare type
        if (!n1.getNodeName().equals(n2.getNodeName()))
            return false;
        // compare attributes
        NamedNodeMap nnm1 = n1.getAttributes();
        NamedNodeMap nnm2 = n2.getAttributes();
        if (nnm1.getLength() != nnm2.getLength())
            return false;
        for (int i = 0; i < nnm1.getLength(); i++) {
            Node attr1 = nnm1.item(i);
            if (!getAttribute(n1, attr1.getNodeName()).equals(getAttribute(n2, attr1.getNodeName())))
                return false;
        }
        // compare children
        Node c1 = n1.getFirstChild();
        Node c2 = n2.getFirstChild();
        for (;;) {
            while ((c1 != null) && c1.getNodeName().startsWith("#"))
                c1 = c1.getNextSibling();
            while ((c2 != null) && c2.getNodeName().startsWith("#"))
                c2 = c2.getNextSibling();
            if ((c1 == null) && (c2 == null))
                break;
            if ((c1 == null) || (c2 == null))
                return false;
            if (!equals(c1, c2))
                return false;
            c1 = c1.getNextSibling();
            c2 = c2.getNextSibling();
        }
        return true;
    }

    /**
     * @param n Node to examine
     * @param attr Attribute to look for
     * @param def Default value to return if attribute is not present
     * @return if the Node contains the named Attribute, the value, if not, the def parameter
     */
    public static String getAttribute(Node n, String attr, String def) {
        NamedNodeMap attrs = n.getAttributes();
        if (attrs == null)
            return def;
        Node ret = attrs.getNamedItem(attr);
        if (ret == null)
            return def;
        else
            return ret.getNodeValue();
    }

    /**
     * @param n Node to examine
     * @param attr Attribute to look for
     * @return if the Node contains the named Attribute, the value, if not, empty string
     */
    public static String getAttribute(Node n, String attr) {
        return getAttribute(n, attr, "");
    }
}

Related

  1. findAllDescendantElements(Node e, String qname, Collection vector)
  2. findAllNodes(Node node, String[] nodeNames, ArrayList results)
  3. findAllProcessingIstructions(Node node, String name, List result)
  4. findAllUrisRecursive(Node node, List nodes)
  5. findElement(Node node, String tagName)
  6. findElement(Node startNode, String name, String namespace)