Java tutorial
//package com.java2s; //License from project: LGPL import java.util.*; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { static Node getElementById(Document doc, String id, Map<String, Node> idMap) { if (!idMap.containsKey(id)) { registerIDs(doc, doc.getDocumentElement(), idMap); } return idMap.get(id); } private static void registerIDs(Document doc, Node node, Map<String, Node> idMap) { if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getAttributes().getNamedItem("id") != null) { final String id = node.getAttributes().getNamedItem("id").getNodeValue(); idMap.put(id, (Element) node); } } NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { registerIDs(doc, children.item(i), idMap); } } }