Here you can find the source of getParentElement(Element element)
Parameter | Description |
---|---|
element | the element |
public static Element getParentElement(Element element)
//package com.java2s; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /**//from w ww . j a v a 2 s . c o m * Gets the parent element. * * @param element * the element * @return the parent element */ public static Element getParentElement(Element element) { if (element == null) { return null; } Element parentElement = null; Node parentNode = element.getParentNode(); while (parentNode != null && parentElement == null) { if (parentNode.getNodeType() == Node.ELEMENT_NODE) { parentElement = (Element) parentNode; } if (parentNode.getNodeType() == Node.DOCUMENT_NODE) { parentElement = ((Document) parentNode).getDocumentElement(); if (element.isSameNode(parentElement)) { parentElement = null; } } parentNode = parentNode.getParentNode(); } return parentElement; } }