Java 如何使用Swing'检索元素的属性;是HTMLEditorKit.ParserCallback吗?
我正在扩展HTMLEditorKit.ParserCallback以解析HTML。我在重写方法中匹配某个元素类型,如下所示:Java 如何使用Swing'检索元素的属性;是HTMLEditorKit.ParserCallback吗?,java,html,swing,Java,Html,Swing,我正在扩展HTMLEditorKit.ParserCallback以解析HTML。我在重写方法中匹配某个元素类型,如下所示: @Override public void handleStartTag(Tag t, MutableAttributeSet a, int pos) { if (Tag.DIV.equals(t)) { String id = (String) a.getAttribute("id"); String clazz = (Strin
@Override
public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
if (Tag.DIV.equals(t)) {
String id = (String) a.getAttribute("id");
String clazz = (String) a.getAttribute("class");
...
在这一行之后,id和clazz始终为null,但我知道事实上,所讨论的元素包含这些属性
如何检索这些属性?可变属性集使用Java 5之前的类型安全枚举模式来表示密钥集。这意味着即使属性名为“class”,仅插入字符串也不会检索属性的值。相反,请使用:
@Override
public void handleStartTag(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);
...
Attribute类包含更多可以匹配的属性
(这让我困惑了一段时间,我在网上搜索时并没有遇到这种用法的例子)