jsoup - 设置属性
以下示例将展示如何使用方法设置 dom 元素的属性、将 HTML 字符串解析为 Document 对象后批量更新和添加/删除类方法。
语法
Document document = Jsoup.parse(html);
Element link = document.select("a").first();
link.attr("href","www.yahoo.com");
link.addClass("header");
link.removeClass("header");
其中
document − document 对象代表 HTML DOM。
Jsoup −主类来解析给定的HTML字符串。
html − HTML字符串。
link − Element对象表示代表锚标记的html节点元素。
link.attr() − attr(attribute,value)方法设置元素属性对应的值。
link.addClass() − addClass(class)方法在class属性下添加类。
link.removeClass() − removeClass(class) 方法删除 class 属性下的类。
描述
Element 对象表示一个 dom 元素,并提供各种方法来获取 dom 元素的属性。
示例
使用您选择的任何编辑器在 C:/> jsoup 中创建以下 java 程序。
JsoupTester.java
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JsoupTester {
public static void main(String[] args) {
String html = "<html><head><title>Sample Title</title></head>"
+ "<body>"
+ "<p>Sample Content</p>"
+ "<div id='sampleDiv'><a id='googleA' href='www.google.com'>Google</a></div>"
+ "<div class='comments'><a href='www.sample1.com'>Sample1</a>"
+ "<a href='www.sample2.com'>Sample2</a>"
+ "<a href='www.sample3.com'>Sample3</a><div>"
+"</div>"
+ "<div id='imageDiv' class='header'><img name='google' src='google.png' />"
+ "<img name='yahoo' src='yahoo.jpg' />"
+"</div>"
+"</body></html>";
Document document = Jsoup.parse(html);
//示例:设置属性
Element link = document.getElementById("googleA");
System.out.println("修改前的外部 HTML :" + link.outerHtml());
link.attr("href","www.yahoo.com");
System.out.println("修改后的外部 HTML :" + link.outerHtml());
System.out.println("---");
//示例:添加类
Element div = document.getElementById("sampleDiv");
System.out.println("修改前的外部 HTML :" + div.outerHtml());
link.addClass("header");
System.out.println("修改后的外部 HTML :" + div.outerHtml());
System.out.println("---");
//示例:删除类
Element div1 = document.getElementById("imageDiv");
System.out.println("修改前的外部 HTML :" + div1.outerHtml());
div1.removeClass("header");
System.out.println("修改后的外部 HTML :" + div1.outerHtml());
System.out.println("---");
//示例:批量更新
Element links = document.select("div.comments a");
System.out.println("修改前的外部 HTML :" + links.outerHtml());
links.attr("rel", "nofollow");
System.out.println("修改前的外部 HTML :" + links.outerHtml());
}
}
验证结果
使用 javac 编译器编译该类,如下所示:
C:\jsoup>javac JsoupTester.java
现在运行 JsoupTester 以查看结果。
C:\jsoup>java JsoupTester
查看结果。
Outer HTML Before Modification :<a id="googleA" href="www.google.com">Google</a> Outer HTML After Modification :<a id="googleA" href="www.yahoo.com">Google</a> --- Outer HTML Before Modification :<div id="sampleDiv"> <a id="googleA" href="www.yahoo.com">Google</a> </div> Outer HTML After Modification :<div id="sampleDiv"> <a id="googleA" href="www.yahoo.com" class="header">Google</a> </div> --- Outer HTML Before Modification :<div id="imageDiv" class="header"> <img name="google" src="google.png"> <img name="yahoo" src="yahoo.jpg"> </div> Outer HTML After Modification :<div id="imageDiv" class=""> <img name="google" src="google.png"> <img name="yahoo" src="yahoo.jpg"> </div> --- Outer HTML Before Modification :<a href="www.sample1.com">Sample1</a> <a href="www.sample2.com">Sample2</a> <a href="www.sample3.com">Sample3</a> Outer HTML Before Modification :<a href="www.sample1.com" rel="nofollow">Sample1</a> <a href="www.sample2.com" rel="nofollow">Sample2</a> <a href="www.sample3.com" rel="nofollow">Sample3</a>

