您的位置:首页 > 编程语言 > Java开发

java 解析 html文档

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

void
flush()

void
, int)]handleComment(char[] data, int pos)

void
handleEndOfLineString(String eol)

它的调用是在完成流的解析之后且在调用
flush
之前。
void
handleEndTag(HTML.Tag t, int pos)

void
handleError(String errorMsg, int pos)

void
handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)

void
handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos)

void
, int)]handleText(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;


}




publicvoid handleComment(char[] data, int pos) ...{


}




publicvoid 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;


}


}




publicvoid handleError(String errorMsg, int pos) ...{


}




publicvoid handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) ...{


handleStartTag(t, a, pos);


}




publicvoid 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;


}


}




publicvoid 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回调类中实现。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: