首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何利用HTMLEditorKit检索HTML的标题

如何利用HTMLEditorKit检索HTML的标题
EN

Stack Overflow用户
提问于 2012-03-06 17:09:48
回答 1查看 1K关注 0票数 3

我想借助java的HTMLEditorKit来检索标题属性。这就是我所写的,但它会一直返回"null“,并且eclipse中的检查器也帮不了什么忙!

代码语言:javascript
复制
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的内容是:

代码语言:javascript
复制
<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>

附言:我知道XPATH,REGEX和任何其他第三方组件可以用一种简单的方式来检索HTML的吸引力,但我也想以艰难的方式学习。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-06 18:35:41

代码语言:javascript
复制
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;
        }
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9580684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档