jsoup - Öznitelikleri ayarla

Aşağıdaki örnek, bir HTML Dizesini Document nesnesine ayrıştırdıktan sonra, bir dom öğesinin niteliklerini ayarlamak için yöntem kullanımını, toplu güncellemeleri ve sınıf yöntemlerini ekleme / kaldırma işlemini gösterecektir.

Sözdizimi

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

Nerede

  • document - belge nesnesi HTML DOM'u temsil eder.

  • Jsoup - verilen HTML Dizesini ayrıştırmak için ana sınıf.

  • html - HTML Dizesi.

  • link - Öğe nesnesi, bağlantı etiketini temsil eden html düğüm öğesini temsil eder.

  • link.attr() - attr (öznitelik, değer) yöntemi, ilgili değeri öğe özniteliğini ayarlar.

  • link.addClass() - addClass (class) yöntemi, sınıfı sınıf özniteliğinin altına ekler.

  • link.removeClass() - removeClass (sınıf) yöntemi, sınıf özelliği altındaki sınıfı kaldırır.

Açıklama

Öğe nesnesi bir etki alanını temsil eder ve bir dom öğesinin özniteliğini elde etmek için çeşitli yöntemler sağlar.

Misal

C: /> jsoup gibi herhangi bir düzenleyiciyi kullanarak aşağıdaki java programını oluşturun.

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

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

Sonucu doğrulayın

Kullanarak sınıfı derleyin javac aşağıdaki gibi derleyici:

C:\jsoup>javac JsoupTester.java

Şimdi sonucu görmek için JsoupTester'ı çalıştırın.

C:\jsoup>java JsoupTester

Sonucu görün.

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>