Skip to content Skip to sidebar Skip to footer

How Do I Retrieve The Attribute Of An Element Using Swing's Htmleditorkit.parsercallback?

I am extending HTMLEditorKit.ParserCallback to parse HTML. I am matching on a certain element type in an overridden method like this: @Override public void handleStartTag(Tag t, Mu

Solution 1:

The MutableAttributeSet uses a pre-Java 5 type-safe enum pattern to represent the key set. This means even though the attribute has the name "class", just inserting the String will not retrieve the value of the attribute. Instead, use:

@OverridepublicvoidhandleStartTag(Tag t, MutableAttributeSet a, int pos) {
    if (Tag.DIV.equals(t)) {
        String id = (String) a.getAttribute(HTML.Attribute.ID);
        String clazz = (String) a.getAttribute(HTML.Attribute.CLASS);
        ...

The HTML.Attribute class contains many more attributes which can be matched on.

(This confused me for a while and I didn't come across example of this usage when searching online).

Post a Comment for "How Do I Retrieve The Attribute Of An Element Using Swing's Htmleditorkit.parsercallback?"