Home > Software design >  How to parse html output to object in kotlin
How to parse html output to object in kotlin

Time:04-08

We get response from a service, they are returning the dropdown options list. I am getting as a string response from api call.

<option value="X" title="XXXX">X</option>
<option value="Y" title="YYYY">Y</option>

I want to parse this to an array of object something like this in kotlin

[
{value:"X",title:"XXXX"},
{value:"Y",title:"YYYY"}
]

Can someone help.

CodePudding user response:

There are few solutions. You can parse your simple HTML using regular expression <option value=\"(. )\" title=\"(. )\">. </option>

// class to hold parsed result
data class Option(val value: String, val title: String) 

fun main() {
    val html = """
        <option value="X" title="XXXX">X</option>
        <option value="Y" title="YYYY">Y</option>
    """

    val regex = "<option value=\"(. )\" title=\"(. )\">. </option>".toRegex()
    
    val regexOptions = regex.findAll(html)
        .map {
            Option(
                value = it.groupValues[1],
                title = it.groupValues[2]
            )
        }.toList()

    println("RegEx: $regexOptions")

or you can use Jsoup library that is designed to parse complex HTML.

    val jsoupOptions = Jsoup.parse(html)
        .select("option")
        .map {
            Option(
                value = it.attr("value"),
                title = it.attr("title")
            )
        }

    println("Jsoup: $jsoupOptions")
}

Output is identical in both cases:

RegEx: [Option(value=X, title=XXXX), Option(value=Y, title=YYYY)]
Jsoup: [Option(value=X, title=XXXX), Option(value=Y, title=YYYY)]
  • Related