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

intfindAllDescendantElements(Node e, String qname, Collection vector)
find All Descendant Elements
int n = 0;
if (e == null || qname == null)
    return 0;
for (Node c = e.getFirstChild(); c != null; c = c.getNextSibling()) {
    if (c.getNodeType() == Node.ELEMENT_NODE && c.getNodeName().equals(qname)) {
        ++n;
        vector.add((Element) c);
    n += findAllDescendantElements(c, qname, vector);
return n;
voidfindAllNodes(Node node, String[] nodeNames, ArrayList results)
find All Nodes
NodeList nodes = node.getChildNodes();
if (nodes != null) {
    for (int i = 0; i < nodes.getLength(); i++) {
        for (int j = 0; j < nodeNames.length; j++) {
            if (nodes.item(i).getNodeName().equals(nodeNames[j])) {
                results.add(nodes.item(i));
        findAllNodes(nodes.item(i), nodeNames, results);
ListfindAllNodesRecursive(Node n, String name)
find All Nodes Recursive
List<Node> nodes = new ArrayList<Node>();
findRecursive(n, name, nodes, false);
return nodes;
voidfindAllProcessingIstructions(Node node, String name, List result)
find All Processing Istructions
NodeList nodeList = node.getChildNodes();
if (nodeList == null) {
    return;
for (int i = 0; i < nodeList.getLength(); ++i) {
    Node n = nodeList.item(i);
    if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        if (name == null || name.length() == 0 || n.getNodeName().equals(name)) {
...
voidfindAllUrisRecursive(Node node, List nodes)
find All Uris Recursive
String url = getSafeAttribute(node, "uri");
if (url != null) {
    nodes.add(node);
NodeList children = node.getChildNodes();
if (children != null) {
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
...
ElementfindElement(Node node, String tagName)
find Element
Element result = null;
NodeList nodeList = node.getChildNodes();
if (nodeList == null) {
    return result;
for (int i = 0; i < nodeList.getLength(); ++i) {
    Element element = checkIfElement(nodeList.item(i), tagName);
    if (element != null) {
...
ElementfindElement(Node startNode, String name, String namespace)
Returns the first element that matches name and namespace.
if (startNode == null) {
    return null;
Node startParent = startNode.getParentNode();
Node processedNode = null;
while (startNode != null) {
    if (startNode.getNodeType() == Node.ELEMENT_NODE && startNode.getLocalName().equals(name)) {
        String ns = startNode.getNamespaceURI();
...
ElementfindElementById(Node head, String id)
Static method to find DOM elements by an ID field
Element element = null;
for (Node node = head.getFirstChild(); node != null; node = node.getNextSibling()) {
    if (node.hasChildNodes()) {
        element = findElementById(node, id);
        if (element != null)
            return element;
    if (node.hasAttributes()) { 
...
ElementfindElementById(Node node, String id)
Find descendant element having specified id attribute.
Node child = node.getFirstChild();
while (child != null) {
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element childElement = (Element) child;
        String value = childElement.getAttributeNS(null, "id");
        if (value != null && value.length() > 0) {
            value = value.trim();
            if (value.equals(id)) {
...
ElementfindElementByName(Node node, final String name)
Look for Element node of given name.
while (node != null) {
    if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name))
        return (Element) node;
    node = node.getNextSibling();
return null;