jsoup - Parsing Body

Following example will showcase parsing an HTML fragement String into a Element object as html body.


Document document = Jsoup.parseBodyFragment(html);
Element body = document.body();


  • document − document object represents the HTML DOM.

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

  • html − HTML fragment String.

  • body − represents element children of the document's body element and is equivalent to document.getElementsByTag("body").


The parseBodyFragment(String html) method parses the input HTML into a new Document. This document object can be used to traverse and get details of the html body fragment.


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


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 = "<div><p>Sample Content</p>";
      Document document = Jsoup.parseBodyFragment(html);
      Element body = document.body();
      Elements paragraphs = body.getElementsByTag("p");
      for (Element paragraph : paragraphs) {

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.

Sample Content