您的位置:首页 > Web前端 > JavaScript

Jsoup 简介 及示例说明

2012-06-07 14:35 155 查看
jsoup 简介:

jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据。

一个文档的对象模型

文档由多个Elements和TextNodes组成 (以及其它辅助nodes:详细可查看:nodes package tree).

其继承结构如下:Document继承Element继承Node. TextNode继承 Node.

一个Element包含一个子节点集合,并拥有一个父Element。他们还提供了一个唯一的子元素过滤列表。

1.解析

Jsoup提供的几个方法:

Jsoup.parse(String html) [html:是html代码]

Jsoup.parse(String html, String baseUri) [baseUri:是用来将相对 URL 转成绝对URL]

Jsoup.parseBodyFragment(String html)

Jsoup.connect(String url) [方法说明:从一个URL加载一个Document对象]

Jsoup.parse(File in, String charsetName, String baseUri) [方法说明:根据一个文件加载Document对象]

Jsoup.parse(File in, String charsetName) [方法说明:根据一个文件加载Document对象]

详细请参考:
http://www.open-open.com/jsoup/parsing-a-document.htm
Jsoup API:
http://jsoup.org/apidocs/
下面是我在项目中遇到的一个例子,贴出来,以供参考:

protected static String getMailTemplate(Map param) throws Exception{
if(param == null) return null;
File emailtemplate = new File(ProductInfo.getWebRealPath() + "\\email_template\\qd.html");
Document doc = Jsoup.parse(emailtemplate , "GBK");

Element codespan = doc.select("#code").first();
codespan.text(param.get("ccode").toString());

Element problemspan = doc.select("#problem").first();
problemspan.text((String)param.get("ctitle"));

Element namespan = doc.select("#name").first();
namespan.text((String)param.get("cchancontacter"));

Element solutionspan = doc.select("#solution").first();
solutionspan.text((String)param.get("ccontent"));

Element solution_versionspan = doc.select("#solution_version").first();
String solutiondesp = (String)param.get("solutiondesp");
if(StringUtil.isBlank(solutiondesp)){
Element versiontitlespan = doc.select("#solution_version_title").first();
versiontitlespan.text("");
} else{
solution_versionspan.text(solutiondesp);
}

Element handlerdiv = doc.select("#handler").first();
handlerdiv.text((String)param.get("chandler"));

Element phonediv = doc.select("#phone").first();
phonediv.text((String)param.get("cofficephone"));

Element emaildiv = doc.select("#email").first();
emaildiv.html("<a href=\"mailto:" + (String)param.get("chandlermail") + "\">" + (String)param.get("chandlermail"));

Element datediv = doc.select("#date").first();
datediv.text((String)param.get("dissuedate"));

String url = "http://" + getSFM(param,"ip") + ":" + getSFM(param,"port") + "/" + getSFM(param,"project") + "/" + getSFM(param,"servlet");
Element form = doc.getElementById("form");
form.attr("action",url.trim());

Element fhd = doc.select("#fhidden").first();
StringBuffer hiddenHtml = new StringBuffer();
hiddenHtml.append("<input type='text' name='fcode' value='" + param.get("ccode").toString() + "' /> ")
 .append("<input type='text' name='facc' value='" + param.get("account").toString() + "' /> ")
 .append("<input type='text' name='furl' value='" + url.trim() + "' /> ")
 .append("<input type='text' name='fws' value='" + param.get("webservice").toString() + "' /> ");
fhd.html(hiddenHtml.toString());

return doc.toString();

}

举例说明:

<form id="form" name="form" action="this is will be replace by the java code" method="post" target="_blank">
<div id="fhidden" style="display:none;"></div>
<div id="header" class="header">
<textarea rows="3" cols="40" name="cRemark"></textarea>
您好,<span id="name"></span>:
</div>
.......

1.doc.select(String div).first();

这个方法是获取html中div,得到Element对象

在Element对象中,可获得Node节点,如textarea

2.hiddenHtml.append(String html)

这个方法可以在指定的div中插入一些元素节点

3.其他常用方法说明

(1) 操作元素数据的方法

getElementById(String id)  [id:html中标签、元素等里的id属性]

getElementsByTag(String tag) 

getElementsByClass(String className)

attr(String key)获取属性

attr(String key, String value)设置属性

attributes()获取所有属性

text()获取文本内容

text(String value) 设置文本内容

html()获取元素内

HTMLhtml(String value)设置元素内的HTML内容

(2) 操作HTML和文本的方法

append(String html)

prepend(String html)

appendText(String text)

prependText(String text)

appendElement(String tagName)

prependElement(String tagName)

html(String value)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: