java 解析 html文档

本文介绍如何使用Java Swing中的HTMLEditorKit解析HTML文档,提取链接和文本内容。通过继承HTMLEditorKit.ParserCallback并实现其方法,可以轻松地从HTML中抓取所需信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用java自带的swing解析html,用起来简单,速度也很快。首先要导入javax.swing.text.*和javax.swing.text.html.*两个包。然后定义一个parser的类,继承了javax.swing.text.html.HTMLEditorKit.ParserCallback这个类,在javax.swing.text.html.HTMLEditorKit.ParserCallback这个类中,有如下几个方法

voidflush()
           
 voidhandleComment(char[] data, int pos)
           
 voidhandleEndOfLineString(String eol)
          它的调用是在完成流的解析之后且在调用 flush 之前。
 voidhandleEndTag(HTML.Tag t, int pos)
           
 voidhandleError(String errorMsg, int pos)
           
 voidhandleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
           
 voidhandleStartTag(HTML.Tag t, MutableAttributeSet a, int pos)
           
 voidhandleText(char[] data, int pos)
  

先拿handleStartTag方法来说,当发现html标签开始的时候调用这个函数,t是标签的名,(比如HTML.Tag.A,这些标签可以在网上查到),a是属性列,比如a标签中的hreg属性,可以通过 HTML.ATTRIBUTE.HREF来拿到。同样,属性列swing也公开了。handleEndTag是当标签结束的时候被调用。用法大家可以看看我写的parser类代码如下

public   class  Parser  extends  ParserCallback  {
 
protected String base;
 
protected boolean isLink = false;
 
protected boolean isParagraph = false;
 
protected boolean isTitle = false;
 
protected String htmlbody = new String();
 
protected String urlTitle = new String();
 
protected Vector<String> links = new Vector<String>();
 
protected Vector<String> linkname = new Vector<String>();
 
protected String paragraphText = new String();
 
protected String linkandparagraph = new String();
 
protected String encode = new String();  
 
public Parser(String baseurl){
  base
=baseurl;
 }

 
public String getEncode() {
  
return encode;
 }

 
public String getURLtille(){
  
return urlTitle;
 }

 
public Vector getLinks()
 
{
     
return links;
 }

 
public Vector getLinkName()
 
{
    
return linkname;
 }

 
public String getParagraphText()
 
{
   
return paragraphText;
 }

 
public String getLinknameAndParagraph()
 
{
    
return linkandparagraph;
 }

 
public void handleComment(char[] data, int pos) {
 }

public void handleEndTag(HTML.Tag t, int pos) {
  
if (t == HTML.Tag.A) {
   
if (isLink) {
    isLink 
= false;
   }

  }
 else if (t == HTML.Tag.P) {
   
if (isParagraph) {
    isParagraph 
= false;
   }

  }
 else if (t == HTML.Tag.TITLE) {
   isTitle 
= false;
  }
 else if (t == HTML.Tag.AREA) {
   isLink 
= false;
  }

 }

 
public void handleError(String errorMsg, int pos) {
 }

 
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
  handleStartTag(t, a, pos);
 }

 
public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
  
// is it some sort of a link
  String href = "";
  
if ((t == HTML.Tag.A) && (t != HTML.Tag.BASE)) {
   href 
= (String) a.getAttribute(HTML.Attribute.HREF);
   
if (href != null{
    
try {
     URL url 
= new URL(new URL(base), href);
     links.addElement(url.toString());
     isLink 
= true;
    }
 catch (MalformedURLException e) {
     
//System.out.println(e.getMessage());
    }

   }

  }
 else if (t == HTML.Tag.AREA) {
   href 
= (String) a.getAttribute(HTML.Attribute.HREF);
   
if (href != null{
    String alt 
= (String) a.getAttribute(HTML.Attribute.ALT);
    
try {
     URL url 
= new URL(new URL(base), href);
     links.addElement(url.toString());
     
if (alt != null{
      linkname.addElement(alt);
      linkandparagraph 
+= alt;
     }

     isLink 
= true;
    }
 catch (MalformedURLException e) {
     
//System.out.println(e.getMessage());
    }

   }

  }
 else if (t == HTML.Tag.TITLE) {
   isTitle 
= true;
  }
 else if (t == HTML.Tag.P) {
   isParagraph 
= true;
  }
 else if (t == HTML.Tag.BASE) {
   href 
= (String) a.getAttribute(HTML.Attribute.HREF);
   
if (href != null)
    base 
= href;
  }

 }

 
public void handleText(char[] data, int pos) {
  
if (isLink) {
   String urlname 
= new String(data);
   
if (urlname != null{
    linkname.addElement(urlname);
    linkandparagraph 
+= urlname;
   }

  }

  
if (isTitle) {
   String temptitle 
= new String(data);
   urlTitle 
= temptitle;
  }

  
if (isParagraph) {
   String tempParagraphText 
= new String(data);
   
if (paragraphText != null{
    paragraphText 
+= tempParagraphText;
    linkandparagraph 
+= tempParagraphText;
   }

  }

 }


其中links存放了超链,paragraphtext存放p标签中的文字,linkandparagraph是链接信息和文本信息。

 

回调类parser写好了,但是还要搞清楚如何使用~~我们需要定义一个类,这个类用来实例化一个HTMLEditorKit.Parser对象,具体代码为

public class HtmlParser extends HTMLEditorKit{
    public HTMLEditorKit.Parser getParser(){
     return super.getParser();
    }
}在定义一个htmltest类,在他的main方法中,代码如下

  HTMLEditorKit.Parser parser=new HtmlParser().getParser();实例化一个parser

  Parser p=new Parser("http://www.xjtu.edu.cn");    初始化回调类
  HTTP http=new HTTPt();           http为自己写的一个类,它可以获得网页源代码
  http.send("http://www.xjtu.edu.cn", null);

 parser.parse(new StringReader(http.getBody()), p, true);   所做的工作都为了这一句话!!!!!

 java.util.Vector link=p.getLinks();
         for(int i=0;i<link.size();i++){
          System.out.println(link.get(i));
         }

这样便得到了xjtu这个网页上的链接,结果如下

http://www.xjtu.edu.cn/en/
http://dean.xjtu.edu.cn/
http://www.xjtu.edu.cn/jdgk/jdgk.htm
http://www.xjtu.edu.cn/jdls/jdls.html
http://www.xjtu.edu.cn/jgsz/jgsz.htm
http://www.xjtu.edu.cn/xjtuer/index.html
http://www.xjtu.edu.cn/rcpy/rcpy.htm
http://www.xjtu.edu.cn/kxyj/kxyj.htm
http://www.xjtu.edu.cn/zsjy/zsjy.htm
http://www.xjtu.edu.cn/yrhj/yrhj.html
http://www.xjtu.edu.cn/ssxz/ssxz.htm
http://www.xjtu.edu.cn/rczp/rczp.htm
http://www.xjtu.edu.cn/hzjl/hzjl.htm
http://www.xjtu.edu.cn/tsda/tsda.htm
http://www.xjtu.edu.cn/kjcy/kjcy.htm
http://www.xjtu.edu.cn/jdxy/jdxy.htm
http://www.xjtu.edu.cn/wlzy/wlzy.htm
http://www.xjtu.edu.cn/fwxd/fwxd.htm
http://xjtunews.xjtu.edu.cn/
http://xjtunews.xjtu.edu.cn/
http://www.xjtu.edu.cn#
https://oa.xjtu.edu.cn/
http://donate.xjtu.edu.cn
http://202.117.38.12/
http://bbs.xjtu.edu.cn/
http://webmail.xjtu.edu.cn/
http://stu.xjtu.edu.cn/
http://www.xjtu.edu.cn/info/info.html
http://202.117.22.184
mailto:xinxigl@mail.xjtu.edu.cn
http://202.117.23.150/enteringweb/
http://gs.xjtu.edu.cn/zhaos/
http://www.xjtlu.edu.cn/

还可以获得链接信息,和文本信息,我就不写出来了,具体都在parser回调类中实现。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值