View RSS Feed

Java XML

DOM Element Attributes

Rate this Entry
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:
Java Code:
String attrValue = element.getAttribute("attrName");
Attr attribute = element.getAttributeNode("attrName");
Most of the time the getAttribute() method will do just fine.
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.

Submit "DOM Element Attributes" to Facebook Submit "DOM Element Attributes" to Digg Submit "DOM Element Attributes" to del.icio.us Submit "DOM Element Attributes" to StumbleUpon Submit "DOM Element Attributes" to Google

Categories
DOM

Comments