【问题标题】:how to retrieve TITLE of a HTML with the help of HTMLEditorKit如何在 HTMLEditorKit 的帮助下检索 HTML 的 TITLE
【发布时间】:2012-03-23 18:04:56
【问题描述】:

我想借助 java 的 HTMLEditorKit 检索 TITLE 属性? 这是我写的,但它会一直返回“null”,并且 Eclipse 中的检查器没有多大帮助!

import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
public class testHTML

{
  public static void main(String args[]) throws Exception 
  {

    Reader reader = new FileReader("C:\\wamp\\www\\t\\index.html");

    new ParserDelegator().parse(reader, new LinkPage(), true);

  }
}
class LinkPage extends HTMLEditorKit.ParserCallback 
{
    public void handleSimpleTag(HTML.Tag tag,
            MutableAttributeSet attributes, int pos) {

        if (tag == HTML.Tag.TITLE)
        {
            System.out.println(attributes.getAttribute(HTML.Attribute.TITLE));
        }
    }
  public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) 
  {
//    if (t == HTML.Tag.A) 
//    {
//      //System.out.println("<BR>");
//      
//    }
//    if(t == HTML.Tag.TITLE)
//    {
//      System.out.println(t.toString());
//      System.out.println(t.TITLE);
//      System.out.println();
//      String text = (String)a.getAttribute(HTML.Attribute.TITLE);
//      Object o = a.getAttribute(HTML.Attribute.TITLE);
//      System.out.println(a);
//      System.out.println(o);
//      System.out.println(text);
//    }
// 
      handleSimpleTag(t, a, pos);
  }
}

HTML 的内容是:

<html>
<head>
<title>test</title>
</head>
<body>
test
<a href="http://localhost/t/1.html">link1</a>
sdf
<a href="http://localhost/t/2.html">link2</a>
sdf
<a href="http://localhost/t/1.html">link3</a>
sdf
<a href="http://localhost/t/2.html">link3</a>
</body>
</html>

p.s : 我知道 XPATH 、 REGEX 和任何其他 3rd 方组件可以轻松地检索 HTML 属性,但我也想学习困难的方法。

【问题讨论】:

    标签: java swing html-parsing htmleditorkit


    【解决方案1】:
    import javax.swing.text.MutableAttributeSet;
    import javax.swing.text.html.HTML;
    import javax.swing.text.html.HTMLEditorKit;
    import javax.swing.text.html.parser.ParserDelegator;
    import java.io.Reader;
    import java.io.StringReader;
    
    public class Test2 {
        public static final String content = "<html> \n" +
                "<head> \n" +
                "<title>test</title> \n" +
                "</head> \n" +
                "<body> \n" +
                "test \n" +
                "<a href=\"http://localhost/t/1.html\">link1</a> \n" +
                "sdf \n" +
                "<a href=\"http://localhost/t/2.html\">link2</a> \n" +
                "sdf \n" +
                "<a href=\"http://localhost/t/1.html\">link3</a> \n" +
                "sdf \n" +
                "<a href=\"http://localhost/t/2.html\">link3</a> \n" +
                "</body> \n" +
                "</html> ";
    
        public static void main(String args[]) throws Exception {
            Reader reader = new StringReader(content);
            new ParserDelegator().parse(reader, new LinkPage(), true);
        }
    }
    
    class LinkPage extends HTMLEditorKit.ParserCallback {
        int startPos = -1;
    
        public void handleText(char[] data, int pos) {
            if (startPos >= 0) {
                startPos = pos;
            }
        }
    
        public void handleEndTag(HTML.Tag t, int pos) {
            super.handleEndTag(t, pos);
            if (t == HTML.Tag.TITLE) {
                System.out.println(Test2.content.substring(startPos, pos));
                startPos = -1;
            }
        }
    
        public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
            super.handleStartTag(t, a, pos);
            if (t == HTML.Tag.TITLE) {
                startPos = pos;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 2017-01-13
      • 1970-01-01
      相关资源
      最近更新 更多