private static Node locateAttrParent(Element elem,Node attr){
  Node parent=null;
  Attr check=elem.getAttributeNode(attr.getNodeName());
  if (check == attr)   parent=elem;
  if (null == parent) {
    for (Node node=elem.getFirstChild(); null != node; node=node.getNextSibling()) {
      if (Node.ELEMENT_NODE == node.getNodeType()) {
        parent=locateAttrParent((Element)node,attr);
        if (null != parent)         break;
      }
    }
  }
  return parent;
}

private static Node locateAttrParent ( Element elem , Node attr ) { Node parent = null ; Attr check = elem . getAttributeNode ( attr . getNodeName ( ) ) ; if ( check == attr ) parent = elem ; if ( null == parent ) { for ( Node node = elem . getFirstChild ( ) ; null != node ; node = node . getNextSibling ( ) ) { if ( Node . ELEMENT_NODE == node . getNodeType ( ) ) { parent = locateAttrParent ( ( Element ) node , attr ) ; if ( null != parent ) break ; } } } return parent ; }
support for getparentofnode ; walks a dom tree until it finds the element which owns the attr . this is hugely expensive , and if at all possible you should use the dom level 2 attr . ownerelement ( ) method instead . < p > the dom level 1 developers expected that folks would keep track of the last element they ' d seen and could recover the info from that source . obviously that doesn ' t work very well if the only information you ' ve been presented with is the attr . the dom level 2 getownerelement ( ) method fixes that , but only for level 2 and later doms .
