【问题标题】:Problem querying an HTML file using HTMLEditorKit in Java在 Java 中使用 HTMLEditorKit 查询 HTML 文件时出现问题
【发布时间】: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”,它位于标签内的超链接内。如何修复解析器以提取日期?

【问题讨论】:

    标签: java html parsing


    【解决方案1】:

    覆盖handleEndTag 并检查"a"

    但是,这个 HTML 解析器是 90 年代初期的,这些方法没有很好地指定。

    【讨论】:

    • 虽然我想了想,但在Strings 上使用== 通常不是一个好主意。
    • 我搜索了 Java 中的 html 解析器,这似乎是一个流行的解析器。如果您知道任何其他易于使用的解析器,如果您能介绍它们,我将不胜感激。
    【解决方案2】:
    import java.io.*;
    import java.util.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    import javax.swing.text.html.parser.*;
    
    public class ParserCallbackDiv extends HTMLEditorKit.ParserCallback
    {
        private boolean isDivLink = false;
        private String divText;
    
        public void handleEndTag(HTML.Tag tag, int pos)
        {
            if (tag.equals(HTML.Tag.DIV))
            {
                System.out.println( divText );
                isDivLink = false;
            }
        }
    
        public void handleStartTag(HTML.Tag tag, MutableAttributeSet a, int pos)
        {
            if (tag.equals(HTML.Tag.DIV))
            {
                String divValue = (String)a.getAttribute(HTML.Attribute.CLASS);
    
                if ("author".equals(divValue))
                    isDivLink = true;
            }
        }
    
        public void handleText(char[] data, int pos)
        {
            divText = new String(data);
        }
    
        public static void main(String[] args)
        throws IOException
        {
            String file = "<div class=\"author\"><a href=\"/user/1\"" +
                "title=\"View user profile.\">Apple</a> - October 22, 2009 - 01:07</div>";
            StringReader reader = new StringReader(file);
    
            ParserCallbackDiv parser = new ParserCallbackDiv();
    
            try
            {
                new ParserDelegator().parse(reader, parser, true);
            }
            catch (IOException e)
            {
                System.out.println(e);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-23
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 2021-11-20
      • 2018-06-07
      • 2021-10-16
      • 1970-01-01
      • 2021-05-13
      相关资源
      最近更新 更多