// import the necessary packages import org.w3c.dom.*; // get the element Element element = document.getElementById("example"); // retrieve the attribute node Attr attribute = element.getAttributeNode("foo"); // print the value of the attribute System.out.println(attribute.getValue());
// import the necessary packages import org.w3c.dom.*; // get the element Element element = document.getElementById("example"); // retrieve the attribute node Attr attribute = element.getAttributeNode("foo"); // set a new value for the attribute attribute.setNodeValue("new value"); // print the updated value of the attribute System.out.println(attribute.getValue());In this example, the setNodeValue() method is used to set a new value for the "foo" attribute of an HTML element with the ID "example". The resulting Attr object is then used to print the updated value of the attribute. Package/Library: org.w3c.dom