jsoup Get HTML Elements by Tag Name in Java

Tags: jsoup HTML Parser

In this post, we are going to use jsoup Java library to find HTML elements by HTML tag name.

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>
    <p>Java Tutorials</p>
    <p>Spring Boot Tutorials</p>
    <p>jsoup Tutorials</p>
</body>
</html>

Example 1 using Document.getElementsByTag() method

To get a list of elements by its tag name we can use Document.getElementsByTag() method of jsoup library.

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

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

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

            Elements elements = document.getElementsByTag("p");

            for(Element element : elements) {
                System.out.println(element.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
Java Tutorials
Spring Boot Tutorials
jsoup Tutorials

Example 2 using Document.select() method

We can use the CSS selector to get matched tag name elements by using Document.select() method.

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

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

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

            Elements elements = document.select("p");

            for(Element element : elements) {
                System.out.println(element.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
Java Tutorials
Spring Boot Tutorials
jsoup Tutorials

Happy Coding 😊

jsoup Get HTML elements by CSS class name in Java

jsoup Get HTML Element by ID 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