jsoup - Setting Text Content



Overview

Element object represent a dom elment and provides various method to set, prepend or append text to a dom element.

Syntax

Document document = Jsoup.parse(html);
Element div = document.getElementById("sampleDiv");
div.text("This is a sample content.");   
div.prepend("Initial Text.");
div.append("End Text.");  

Where

  • document − document object represents the HTML DOM.

  • Jsoup − main class to parse the given HTML String.

  • html − HTML String.

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

  • div.text() − text(content) method replaces the element's content with the corresponding value.

  • div.prepend() − prepend(content) method adds the content before the outer html.

  • div.append() − append(content) method adds the content after the outer html.

Example - Modifying Content

JsoupTester.java

package com.tutorialspoint;

import java.io.IOException;

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

public class JsoupTester {
   public static void main(String[] args) throws IOException {
   
      String html = "<html><head><title>Sample Title</title></head>"
         + "<body>"
         + "<div id='sampleDiv'><a id='googleA' href='www.google.com'>Google</a></div>"       
         +"</body></html>";
      Document document = Jsoup.parse(html);

      Element div = document.getElementById("sampleDiv");
      System.out.println("Outer HTML Before Modification :\n"  + div.outerHtml());
      div.text("This is a sample content.");
      System.out.println("Outer HTML After Modification :\n"  + div.outerHtml());
   }
}

Verify the result

Compile and run the JsoupTester to verify the result −

Outer HTML Before Modification :
<div id="sampleDiv">
 <a id="googleA" href="www.google.com">Google</a>
</div>
Outer HTML After Modification :
<div id="sampleDiv">This is a sample content.</div>

Example - Prepending Text Content

JsoupTester.java

package com.tutorialspoint;

import java.io.IOException;

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

public class JsoupTester {
   public static void main(String[] args) throws IOException {
   
      String html = "<html><head><title>Sample Title</title></head>"
         + "<body>"
         + "<div id='sampleDiv'><a id='googleA' href='www.google.com'>Google</a></div>"       
         +"</body></html>";
      Document document = Jsoup.parse(html);

      Element div = document.getElementById("sampleDiv");
      System.out.println("Outer HTML Before Modification :\n"  + div.outerHtml());
	  
	  div.prepend("Initial Text.");
      System.out.println("After Prepend :\n"  + div.outerHtml());
   }
}

Verify the result

Compile and run the JsoupTester to verify the result −

Outer HTML Before Modification :
<div id="sampleDiv">
 <a id="googleA" href="www.google.com">Google</a>
</div>
After Prepend :
<div id="sampleDiv">
 Initial Text.<a id="googleA" href="www.google.com">Google</a>
</div>

Example - Appending Text Content

JsoupTester.java

package com.tutorialspoint;

import java.io.IOException;

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

public class JsoupTester {
   public static void main(String[] args) throws IOException {

      String html = "<html><head><title>Sample Title</title></head>"
         + "<body>"
         + "<div id='sampleDiv'><a id='googleA' href='www.google.com'>Google</a></div>"       
         +"</body></html>";
      Document document = Jsoup.parse(html);

      Element div = document.getElementById("sampleDiv");
      System.out.println("Outer HTML Before Modification :\n"  + div.outerHtml());
      div.append("End Text.");
      System.out.println("After Append :\n"  + div.outerHtml());     
   }
}

Verify the result

Compile and run the JsoupTester to verify the result −

Outer HTML Before Modification :
<div id="sampleDiv">
 <a id="googleA" href="www.google.com">Google</a>
</div>
After Prepend :
<div id="sampleDiv">
 Initial Text.<a id="googleA" href="www.google.com">Google</a>
</div>
Advertisements