Here you can find the source of findElementWithId(String id, Element root)
Parameter | Description |
---|---|
root | the root Element of the search. search will cover root and its descendants |
id | the id to search for |
public static Element findElementWithId(String id, Element root)
//package com.java2s; /*//w ww. j ava 2 s. co m * ==================================================================== * This software is subject to the terms of the Common Public License * Agreement, available at the following URL: * http://www.opensource.org/licenses/cpl.html . * Copyright (C) 2003-2004 TONBELLER AG. * All Rights Reserved. * You must accept the terms of that agreement to use this software. * ==================================================================== * * */ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * fast search for Element with id attribute * @param root the root Element of the search. search will cover root and * its descendants * @param id the id to search for * @return null or the element */ public static Element findElementWithId(String id, Element root) { if (id.equals(root.getAttribute("id"))) return root; NodeList list = root.getChildNodes(); int len = list.getLength(); for (int i = 0; i < len; i++) { Node n = list.item(i); if (n.getNodeType() != Node.ELEMENT_NODE) continue; Element child = (Element) list.item(i); Element found = findElementWithId(id, child); if (found != null) return found; } return null; } }