Here you can find the source of findElementById(Node node, String id)
Parameter | Description |
---|---|
node | <em>document</em> or element node being searched |
id | value of the id attribute |
nnull
public static Element findElementById(Node node, String id)
//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; } }