DOM Element Attributes
by, 04-27-2012 at 05:12 PM (520 Views)
As you have already seen, you can access the attributes of an element via the Element interface. There are two ways to do so:
Most of the time the getAttribute() method will do just fine.Java Code:String attrValue = element.getAttribute("attrName"); Attr attribute = element.getAttributeNode("attrName");
The Attr interface extends Node. It allows you to access the owning element via the methodgetOwnerElement() etc. Accessing an attribute via this interface is mostly handy if you need to pass the attribute to one or more methods, where the method needs to access more info about the attribute in order to process it.
There is a lot more you can do with the Document object and the related nodes, but accessing child elements and attributes are what you will be using 90% of the time. The rest you can find by checking out the JavaDoc's. Sooner or later you will have to do that anyways.