public static int isAncestorOf(Element ancestor,Element maybeChild){
  int ret=-1;
  if (ancestor == maybeChild) {
    ret=0;
  }
  Node parent=maybeChild.getParentNode();
  int i=1;
  while (parent != null && parent != ancestor) {
    parent=parent.getParentNode();
    ++i;
  }
  if (parent == ancestor) {
    ret=i;
  }
  return ret;
}

public static int isAncestorOf ( Element ancestor , Element maybeChild ) { int ret = - 1 ; if ( ancestor == maybeChild ) { ret = 0 ; } Node parent = maybeChild . getParentNode ( ) ; int i = 1 ; while ( parent != null && parent != ancestor ) { parent = parent . getParentNode ( ) ; ++ i ; } if ( parent == ancestor ) { ret = i ; } return ret ; }
checks if ancestor - element is an ancestor of maybechild element
