jsoup Get HTML Element by ID in Java

Tags: jsoup HTML Parser

In this post, we will learn how to find an element in an HTML document by its ID in a Java application using jsoup library.

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 the sample.html file as below.

<!DOCTYPE html>
<html>
<body>
    <h1 id="article-title">Simple Solution</h1>
</body>
</html>

Example 1 using Document.getElementById() method

To find an element by ID in jsoup we can use Document.getElementById() method.

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

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

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

            Element element = document.getElementById("article-title");

            System.out.println("Text: " + element.text());
            System.out.println("Tag Name: " + element.tagName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
Text: Simple Solution
Tag Name: h1

Example 2 using Document.selectFirst() method

Given an ID of element we can get the element by using CSS selector with Document.selectFirst() method to return one 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 GetElementsByIdExample2 {
    public static void main(String... args) {
        try {
            File file = new File("sample.html");
            Document document = Jsoup.parse(file, "UTF-8");

            Element element = document.selectFirst("#article-title");

            System.out.println("Text: " + element.text());
            System.out.println("Tag Name: " + element.tagName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
Text: Simple Solution
Tag Name: h1

Example 3 using Document.select() method

We also can use Document.select() that returns a list of Elements in the result and then get the first one with the first() method.

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

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

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

            Element element = document.select("#article-title").first();

            System.out.println("Text: " + element.text());
            System.out.println("Tag Name: " + element.tagName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
Text: Simple Solution
Tag Name: h1

Happy Coding 😊

jsoup Get HTML elements by CSS class name in Java

jsoup Get HTML Elements by Tag Name in Java

jsoup Get HTML Elements by Attribute Name in Java

jsoup Get HTML Elements by Attribute Value in Java

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

jsoup Get All HTML Elements in Java