gpt4 book ai didi

java - 在 Java 中使用 HTMLEditorKit 查询 HTML 文件时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:10 24 4
gpt4 key购买 nike

我的 HTML 包含以下形式的标签:

<div class="author"><a href="/user/1" title="View user profile.">Apple</a> - October 22, 2009 - 01:07</div>

我想从每个标签中提取日期“2009 年 10 月 22 日 - 01:07”

我已经实现了 javax.swing.text.html.HTMLEditorKit.ParserCallback 如下:

class HTMLParseListerInner extends HTMLEditorKit.ParserCallback {   
private ArrayList<String> foundDates = new ArrayList<String>();
private boolean isDivLink = false;

public void handleText(char[] data, int pos) {
if(isDivLink)
foundDates.add(new String(data)); // Extracts "Apple" instead of the date.
}

public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
String divValue = (String)a.getAttribute(HTML.Attribute.CLASS);
if (t.toString() == "div" && divValue != null && divValue.equals("author"))
isDivLink = true;
}
}

但是,上面的解析器返回“Apple”,它位于标记内的超链接内。如何修复解析器以提取日期?

最佳答案

覆盖handleEndTag并检查“a”

但是,这个 HTML 解析器是 90 年代初的产物,这些方法没有明确指定。

关于java - 在 Java 中使用 HTMLEditorKit 查询 HTML 文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1677991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com