Search code examples
kotlinkotlin-multiplatform

How to parse html in KMM?


I'm making a multiplatform app for Android and iOS. I need to parse the HTML page. On android I could use Jsoup, but Jsoup does not support KMM. What should I use better? Or do I need to write two implementations for Android and Ios?


Solution

  • If you are looking for a library to parse HTML and XML with Kotlin Multiplatform, I recommend checking out library fleeksoft/Ksoup. You can find it at GitHub repository: https://github.com/fleeksoft/ksoup. It's specifically tailored for Kotlin Multiplatform environments, offering functionalities similar to those of java jsoup. This library might be particularly useful for your use case, especially if you're familiar with jsoup's capabilities and looking for a similar experience in a Kotlin Multiplatform project.

    Here is sample code:

    // parseGetRequest is suspend function can use also use parseGetRequestBlocking
        val doc: Document = Ksoup.parseGetRequest(url = "https://en.wikipedia.org/")
        // val doc: Document = Ksoup.parse(html = "HTML CODE")
        println("title: ${doc.title()}")
        val headlines: Elements = doc.select("#mp-itn b a")
        
        headlines.forEach { headline: Element ->
            val headlineTitle = headline.attr("title")
            val headlineLink = headline.absUrl("href")
        
            println("$headlineTitle => $headlineLink")
        }