Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**
     * @param element The element whose ancestry we will check
     * @param tagName The tagName of the element we are searching for in the ancestry
     * @param limitTagName Stop searching if we hit this limit
     * @return The first matching element, if found
     */
    public static Element getAncestorOrSelf(final Element element, final String tagName,
            final String limitTagName) {
        Element result = null;
        Element next = element;
        String currentTagName;
        do {
            currentTagName = next.getTagName();
            if (currentTagName.equals(tagName)) {
                result = next;
                break;
            } else {
                Node parent = next.getParentNode();
                if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
                    next = (Element) parent;
                } else {
                    break;
                }
            }
        } while (next != null && (limitTagName == null || !tagName.equals(limitTagName)));
        return result;
    }
}