code: private static Node locateAttrParent(Element elem, Node attr)
    {
  
      Node parent = null;
  
          // This should only be called for Level 1 DOMs, so we don't have to
          // worry about namespace issues. In later levels, it's possible
          // for a DOM to have two Attrs with the same NodeName but
          // different namespaces, and we'd need to get getAttributeNodeNS...
          // but later levels also have Attr.getOwnerElement.
          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;
    }
docs: 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.
  
  @param elem Element whose subtree is to be searched for this Attr
  @param attr Attr whose owner is to be located.
  
  @return the first Element whose attribute list includes the provided
  attr. In modern DOMs, this will also be the only such Element. (Early
  DOMs had some hope that Attrs might be sharable, but this idea has
  been abandoned.)