Node node = document.getDocumentElement(); String id = node.getAttribute("id"); System.out.println("The ID of this node is: " + id);
Element newElement = document.createElement("newElement"); newElement.setAttribute("id", "1234");In this example, we are creating a new element and assigning it a unique identifier using the Node.setAttribute() method. This ID can then be used to reference the element later on. The Node class is part of the Java DOM (Document Object Model) API and can be found in the javax.xml.tree package.