Jsoup-set-attributes

提供:Dev Guides
移動先:案内検索

jsoup-属性を設定する

次の例では、HTML文字列をDocumentオブジェクトに解析した後、dom要素の属性を設定するメソッドの使用、一括更新、クラスメソッドの追加/削除を紹介します。

構文

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

どこで

  • document -ドキュメントオブジェクトはHTML DOMを表します。
  • Jsoup -指定されたHTML文字列を解析するメインクラス。
  • html -HTML文字列。
  • link -要素オブジェクトは、アンカータグを表すhtmlノード要素を表します。
  • * link.attr()*-attr(attribute、value)メソッドは、要素の属性に対応する値を設定します。
  • * link.addClass()*-addClass(class)メソッドは、クラス属性の下にクラスを追加します。
  • * link.removeClass()*-removeClass(class)メソッドは、class属性の下のクラスを削除します。

説明

要素オブジェクトは、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);

     //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());
   }
}

結果を検証する

次のように 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>