Java tutorial
//package com.java2s; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** * Traverses the passed Document <var>doc</var> and sets the "id" attribute * to be the user-defined ID attribute. This is used to find elements by * their ID. <br> * Specifically, this method simply gets the Document Element from * <var>doc</var> and passes it to defineIDAttribute(). * * @see XMLUtil#defineIDAttribute(Element) * @param doc */ public static void defineIDAttribute(Document doc) { Element e = doc.getDocumentElement(); if (e != null) defineIDAttribute(e); } /** * Traverses the passed Element <var>e</var> and sets the "id" attribute to * be the user-defined ID attribute. This method recursively visits all * children of the parent Element <var>e</var>. This is used to find * elements by their ID * * @param e */ private static void defineIDAttribute(Element e) { if (e.getAttribute("id").length() > 0) e.setIdAttribute("id", true); for (Node child = e.getFirstChild(); child != null; child = child.getNextSibling()) if (child.getNodeType() == Document.ELEMENT_NODE) defineIDAttribute((Element) child); } }