question regarding Element.getAttributeNS()

Thanks to everyone who replied earlier to clear up that attributes with no
namespace prefix specified will have a null namespace URI under DOM Level 2
(unlike elements, which inherit a namespace URI).

This worries me a bit regarding how one would use the DOM Level 2 in
practice. Let's say I have the following document:

<myDocument xmlns="www.me.com" xmlns:myNS="www.me.com" >
  <myElement myAttr="1"/>
  <myElement myNS:myAttr="1"/>
</myDocument>

Using the DOM Level 2, how can I elegantly get the value of the myElement
"a" attribute? I can't use myElement.getAttributeNS(null, "myAttr"), because
that would work for the first nested element, not for the second. I can't
use myElement.getAttributeNS("www.me.com", "myAttr"), because that would
work for the second element, and not the first. I would instead have to use
Java code something like the following *every* time I want to get an
attribute using the DOM Level 2:

public Attr findAttributeNS(Element element, String namespaceURI, String
localName)
{
  Attr attr=element.getAttributeNS(namespaceURI, localName);
  if(attr==null)
  {
    if(element.getNamespaceURI().equals(namespaceURI))
    {
      attr=element.getAttributeNS(null, localName);
    }
  }
  return attr;
}

This to me seems very inelegant and inefficient. Is this how it's supposed
to work?

Garret

Received on Friday, 22 December 2000 10:02:07 UTC