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这个类中,有如下几个方法
先拿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回调类中实现。
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) |
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回调类中实现。
相关文章推荐
- JAVA中使用Htmlparse解析HTML文档
- 解析html文档的java库及范例
- 解析html文档的java库及范例
- Java中使用Jsoup对Html文档进行解析和操作
- java解析zip文档,解决解析zip中解析xml和html文档流断的方法
- JAVA中使用Htmlparse解析HTML文档,使用htmlparse遍历出HTML文档的所有超链接(标记)
- 自己动手写搜索引擎(常搜吧历程七#解析文档之HTML#)(Java、Lucene、hadoop)
- JAVA大字符串存储采用静态HTML,通过jsoup 对 HTML 文档进行解析和操作
- java 解析 office系列文档
- Java发送Http请求,解析html返回
- Java解析XML文档——dom解析xml
- 非常好用的一个Html解析的java类库 Jsoup
- [置顶] 使用 jsoup 对 HTML 文档进行解析和操作
- java的反射(构造函数,属性,方法)以及JAXP解析DOM文档
- Jsoup解析HTML实例及文档方法详解
- java如何解决解析html的单引号嵌套
- Jsoup-解析Html文档
- jsoup 对 HTML 文档进行解析和操作
- 记录将文档word2007版本转换为html格式——java代码
- Java实现HTML代码生成PDF文档