Parsing HTML in Kotlin: Library Overview and Practical Examples
Kotlin is a modern multiplatform language actively used for developing server applications, Android apps, and automation tools. One common task is HTML parsing — extracting data from web pages, working with the DOM tree, or generating HTML templates. In this article, we will review the main libraries for parsing HTML in Kotlin, their features, and provide working code examples.
Why is HTML Parsing Needed in Kotlin?
HTML parsing is required in scenarios such as:
- Collecting data from websites (web scraping).
- Automating web interface testing.
- Generating HTML reports or email templates.
- Processing and analyzing content obtained from APIs or files.
Thanks to Java compatibility and its own DSL constructs, Kotlin offers both proven Java libraries and native solutions.
1. Jsoup — Universal HTML/XML Parser
Jsoup is the most popular library for parsing HTML in the JVM ecosystem. It provides a convenient API for loading, parsing, and manipulating HTML documents, supporting CSS selectors and jQuery-like methods. Jsoup works perfectly with Kotlin due to full Java compatibility and the ability to use Kotlin extensions.
Installation (Gradle)
dependencies { implementation("org.jsoup:jsoup:1.17.2")}Example: Extracting Article Titles from a Blog
import org.jsoup.Jsoupimport org.jsoup.nodes.Document
fun main() { // Load HTML document by URL val doc: Document = Jsoup.connect("https://example.com/blog").get()
// Extract all h2 headings with class "post-title" val titles = doc.select("h2.post-title")
// Print the text of each heading titles.forEach { title -> println(title.text()) }
// Example using CSS selector: all links inside articles val links = doc.select("article a[href]") links.forEach { link -> println("${link.text()} -> ${link.attr("href")}") }}Jsoup also allows parsing HTML from a string, file, or InputStream, as well as cleaning HTML from potentially dangerous elements (XSS protection).
2. kotlinx.html — Type-Safe DSL for Working with HTML
kotlinx.html is an official JetBrains library designed for generating HTML code using a type-safe DSL. Unlike Jsoup, it does not parse existing HTML but creates it. However, it can be combined with Jsoup for a full cycle: parsing → modification → generation.
Installation (Gradle)
dependencies { implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.11.0")}Example: Generating an HTML Page
import kotlinx.html.*import kotlinx.html.stream.createHTML
fun main() { val htmlContent = createHTML().html { head { title("Example Page") meta(charset = "UTF-8") } body { h1 { +"Hello, Kotlin!" } p { +"This is a paragraph generated using " b { +"kotlinx.html" } } ul { li { +"Item 1" } li { +"Item 2" } li { +"Item 3" } } } } println(htmlContent)}This approach is especially useful for creating reports, email templates, or dynamic server-side pages (Ktor).
3. Ktor Client + Jsoup — for Parsing with HTTP Requests
If you need to perform complex HTTP requests (with cookies, headers, authentication) before parsing, it is convenient to use Ktor Client together with Jsoup. Ktor is an asynchronous HTTP client that integrates well with Kotlin coroutines.