jsoup Get HTML Elements by Attribute Name in Java

Tags: jsoup HTML Parser

In this post, we are going to use the jsoup library to find HTML elements by attribute name in a Java program.

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>
    <span data-test1="test">Simple Solution</span>
    <span data-test2="test">Java Tutorials</span>
    <span data-test3="test">Spring Boot Tutorials</span>
</body>
</html>

Example 1 using Document.getElementsByAttribute() method

To find all HTML elements by attribute name using jsoup library we can use Document.getElementsByAttribute() 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 GetElementsByAttributeExample1 {
    public static void main(String... args) {
        try {
            File file = new File("sample.html");
            Document document = Jsoup.parse(file, "UTF-8");

            Elements elements = document.getElementsByAttribute("data-test1");

            for(Element element : elements) {
                System.out.println(element.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
Simple Solution

Example 2 using Document.getElementsByAttributeStarting() method

We also can find HTML elements with a given prefix String with Document.getElementsByAttributeStarting() 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 GetElementsByAttributeExample2 {
    public static void main(String... args) {
        try {
            File file = new File("sample.html");
            Document document = Jsoup.parse(file, "UTF-8");

            Elements elements = document.getElementsByAttributeStarting("data-test");

            for(Element element : elements) {
                System.out.println(element.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
Simple Solution
Java Tutorials
Spring Boot 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 Tag 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