Java XML Node Find findElementById(Node node, String id)

Here you can find the source of findElementById(Node node, String id)

Description

Find descendant element having specified id attribute.

License

Open Source License

Parameter

Parameter Description
node <em>document</em> or element node being searched
id value of the id attribute

Return

found element or nnull

Declaration

public static Element findElementById(Node node, String id) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class Main {
    /**/* www.  j a va 2s. co m*/
     * Find descendant element having specified id attribute.
     * 
     * @param node <em>document</em> or element node being searched
     * @param id value of the id attribute
     * @return found element or <code>nnull</code>
     */
    public static Element findElementById(Node node, String id) {
        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)) {
                        return childElement;
                    }
                }

                Element found = findElementById(childElement, id);
                if (found != null) {
                    return found;
                }
            }

            child = child.getNextSibling();
        }

        return null;
    }
}

Related

  1. findAllProcessingIstructions(Node node, String name, List result)
  2. findAllUrisRecursive(Node node, List nodes)
  3. findElement(Node node, String tagName)
  4. findElement(Node startNode, String name, String namespace)
  5. findElementById(Node head, String id)
  6. findElementByName(Node node, final String name)
  7. findElementByTag(Set nodes, String tag)
  8. findElementNodeByName(Node node, String name)
  9. findElements(Node fromnode, String name)