import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; Element element = … // obtain element object NamedNodeMap attributes = element.getAttributes(); // get attributes of the current element for (int i = 0; i < attributes.getLength(); i++) { Node attribute = attributes.item(i); String attributeName = attribute.getNodeName(); String attributeValue = attribute.getNodeValue(); System.out.println(attributeName + " = " + attributeValue); }
import org.w3c.dom.Element; Element element = … // obtain element object element.setAttribute("id", "new-id-value"); // set value of "id" attributeIn this example, we obtain an element object and set the value of the "id" attribute to a new value. Package library: This functionality is provided through the Java DOM API, which is part of the Java Standard Library. Therefore, no external package or library is required.