【发布时间】:2009-11-05 11:42:13
【问题描述】:
我的 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”,它位于标签内的超链接内。如何修复解析器以提取日期?
【问题讨论】: