File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes fun with getAttribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "fun with getAttribute" Watch "fun with getAttribute" New topic

fun with getAttribute

Mark Ayad

Joined: Sep 06, 2001
Posts: 3
Hi All,
Im having problems with this snippet of code. I'm trying
to get all the key value pairs for a html tag element using
public void showAttributes(MutableAttributeSet attrSet)
Enumeration attrEnum = attrSet.getAttributeNames();

String s = (attrEnum.nextElement().toString());

if(s != null)
attrSet.getAttribute(s); !!always returns null!!
I can Enumeration the AttributeSet which brings back all
the attribute names (s) contained in the tag. However
attrSet.getAttribute(s); !!always returns null!!.
Looking a bit deeper say the tag is < a href="index.htm" target="blank">
then the attribute names is:
now you would thought that attrSet.getAttribute("href") would bring
back index.htm, but no it returns null.
So I tried attrSet.getAttribute(HTML.Attribute.HREF) which worked.
....And so it seems that if the attribute name is defined in Class HTML.Attribute
then you can't obtain the attribute value using attrSet.getAttribute(s).
However if the attribute name is not present in Class HTML.Attribute then
attrSet.getAttribute(s) works fine.
One very bad solution is to use a lookup
return HTML.Attribute.ACTION;
else if(name.equals("align"))
return HTML.Attribute.ALIGN;
else if(name.equals("alink"))
return HTML.Attribute.ALINK;

.... but this is a log if - else ladder

Any ideas??


[This message has been edited by Jim Yingst (edited October 11, 2001).]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
The method you want is in the class javax.swing.text.html.HTML:
HTML.Attribute attr = HTML.getAttributeKey(name);
return attr;

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: fun with getAttribute
jQuery in Action, 3rd edition