Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.List; import java.util.ListIterator; import org.w3c.dom.Node; public class Main { private static Node getNearestAncestorNode(Node node, List<Node[]> list) { for (ListIterator<Node[]> i = list.listIterator(list.size()); i.hasPrevious();) { Node[] nodes = i.previous(); if (isDescendant(node, nodes[0])) { return nodes[1]; } } return null; } public static boolean isDescendant(Node testNode, Node compareToNode) { if (testNode != null && compareToNode != null) { while (testNode.getParentNode() != null) { if (testNode.getParentNode() == compareToNode) { return true; } testNode = testNode.getParentNode(); } } return false; } }