Element childElement = (Element) parentElement.getFirstChild(); Node parentNode = childElement.getParentNode();
Document doc = builder.parse("file.xml"); NodeList nodeList = doc.getElementsByTagName("employee"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); Node parentNode = node.getParentNode(); }In this example, we first parse an XML document and then get a list of all the "employee" elements in the document. For each element, we get its parent node using the getParentNode() method. The DOM API is a part of the javax.xml package library in Java.