Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /**
     * 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;
    }
}