Accessing attributes of an element
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; public class Main { public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder loader = factory.newDocumentBuilder(); Document document = loader.parse("sample.xml"); Element purchaseOrder = document.getDocumentElement(); Attr orderDate = purchaseOrder.getAttributeNode("date"); System.out.println(orderDate.getValue()); NamedNodeMap attrs = purchaseOrder.getAttributes(); int attrsCount = attrs.getLength(); for (int i = 0; i < attrsCount; i++) { Attr item = (Attr) attrs.item(i); System.out.println("'" + item.getName() + "' = '" + item.getValue() + "'"); } } }