jsoup extract Inner and Outer HTML of HTML Element in Java

Tags: Java jsoup HTML Parser

In this post, we learn how to use jsoup Java library to extract inner or outer HTML of an element in a HTML document.

Add jsoup library to your Java project

To use jsoup Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile 'org.jsoup:jsoup:1.13.1'

To use jsoup Java library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.13.1</version>
</dependency>

To download the jsoup-1.13.1.jar file you can visit jsoup download page at jsoup.org/download

Sample HTML File

For example, we have a sample.html file as below.

<!DOCTYPE html>
<html>
<body>
    <div id="container">
        <h1>Simple Solution</h1>
    </div>
</body>
</html>

Extract Inner HTML of an Element

The jsoup library provides the Element.html() method to get the HTML inner of an element.

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

import java.io.File;
import java.io.IOException;

public class ExtractInnerHTMLExample {
    public static void main(String... args) {
        try {
            String fileName = "sample.html";
            File file = new File(fileName);
            Document document = Jsoup.parse(file, "UTF-8");
            Element element = document.getElementById("container");

            String innerHtml = element.html();

            System.out.println(innerHtml);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
<h1>Simple Solution</h1>

Extract Outer HTML of an Element

To get outer HTML of a HTML element we can use Element.outerHtml() as the following example code.

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

import java.io.File;
import java.io.IOException;

public class ExtractOuterHTMLExample {
    public static void main(String... args) {
        try {
            String fileName = "sample.html";
            File file = new File(fileName);
            Document document = Jsoup.parse(file, "UTF-8");
            Element element = document.getElementById("container");

            String outerHtml = element.outerHtml();

            System.out.println(outerHtml);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
<div id="container"> 
 <h1>Simple Solution</h1> 
</div>

Happy Coding 😊

jsoup extract CSS class name of HTML element in Java

jsoup extract ID and name of HTML element in Java

jsoup extract text and attributes of HTML element in Java

jsoup extract JavaScript from HTML script element in Java

jsoup extract custom data attributes of HTML5 Element in Java

jsoup extract Website Title in Java

jsoup parse HTML Document from a Java String

jsoup parse HTML Document from an URL in Java

jsoup parse HTML Document from a File and InputStream in Java

Pretty Printing HTML String in Java using jsoup

Extract All Links of a web page in Java using jsoup

jsoup Get HTML elements by CSS class name in Java

Clean HTML String to get Safe HTML from Untrusted HTML in Java using jsoup

jsoup Get All HTML Elements in Java