English 中文(简体)
jsoup - Set Attributes
  • 时间:2024-12-22

jsoup - Set Attributes


Previous Page Next Page  

Following example will showcase use of method to set attributes of a dom element, bulk updates and add/remove class methods after parsing an HTML String into a Document object.

Syntax

Document document = Jsoup.parse(html);
Element pnk = document.select("a").first();         
pnk.attr("href","www.yahoo.com");     
pnk.addClass("header"); 
pnk.removeClass("header");    

Where

    document − document object represents the HTML DOM.

    Jsoup − main class to parse the given HTML String.

    html − HTML String.

    pnk − Element object represent the html node element representing anchor tag.

    pnk.attr() − attr(attribute,value) method set the element attribute the corresponding value.

    pnk.addClass() − addClass(class) method add the class under class attribute.

    pnk.removeClass() − removeClass(class) method remove the class under class attribute.

Description

Element object represent a dom elment and provides various method to get the attribute of a dom element.

Example

Create the following java program using any editor of your choice in say C:/> jsoup.

JsoupTester.java

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

pubpc class JsoupTester {
   pubpc static void main(String[] args) {
   
      String html = "<html><head><title>Sample Title</title></head>"
         + "<body>"
         + "<p>Sample Content</p>"
         + "<span id= sampleDiv ><a id= googleA  href= www.google.com >Google</a></span>"
         + "<span class= comments ><a href= www.sample1.com >Sample1</a>"
         + "<a href= www.sample2.com >Sample2</a>"
         + "<a href= www.sample3.com >Sample3</a><span>"
         +"</span>"
         + "<span id= imageDiv  class= header ><img name= google  src= google.png  />"
         + "<img name= yahoo  src= yahoo.jpg  />"
         +"</span>"
         +"</body></html>";
      Document document = Jsoup.parse(html);

      //Example: set attribute
      Element pnk = document.getElementById("googleA");
      System.out.println("Outer HTML Before Modification :"  + pnk.outerHtml());
      pnk.attr("href","www.yahoo.com");      
      System.out.println("Outer HTML After Modification :"  + pnk.outerHtml());
      System.out.println("---");
      
      //Example: add class
      Element span = document.getElementById("sampleDiv");
      System.out.println("Outer HTML Before Modification :"  + span.outerHtml());
      pnk.addClass("header");      
      System.out.println("Outer HTML After Modification :"  + span.outerHtml());
      System.out.println("---");
      
      //Example: remove class
      Element span1 = document.getElementById("imageDiv");
      System.out.println("Outer HTML Before Modification :"  + span1.outerHtml());
      span1.removeClass("header");      
      System.out.println("Outer HTML After Modification :"  + span1.outerHtml());
      System.out.println("---");
      
      //Example: bulk update
      Elements pnks = document.select("span.comments a");
      System.out.println("Outer HTML Before Modification :"  + pnks.outerHtml());
      pnks.attr("rel", "nofollow");
      System.out.println("Outer HTML Before Modification :"  + pnks.outerHtml());
   }
}

Verify the result

Compile the class using javac compiler as follows:

C:jsoup>javac JsoupTester.java

Now run the JsoupTester to see the result.

C:jsoup>java JsoupTester

See the result.

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 :<span id="sampleDiv">
 <a id="googleA" href="www.yahoo.com">Google</a>
</span>
Outer HTML After Modification :<span id="sampleDiv">
 <a id="googleA" href="www.yahoo.com" class="header">Google</a>
</span>
---
Outer HTML Before Modification :<span id="imageDiv" class="header">
 <img name="google" src="google.png">
 <img name="yahoo" src="yahoo.jpg">
</span>
Outer HTML After Modification :<span id="imageDiv" class="">
 <img name="google" src="google.png">
 <img name="yahoo" src="yahoo.jpg">
</span>
---
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>
Advertisements