Search code examples
androidhtmlkotlinjsoup

Extract the text of same html tags in separate lines with JSoup on Android


I have this html code:

<li itemprop="something">Text 1</li><li itemprop="something">Text 2</li><li itemprop="something">Text 3</li><li itemprop="something">Text 4</li><li itemprop="something">Text 5 </li><li itemprop="something">Text 6 </li>

When I use the following code to extract the text, it gives me them continuous.

val doc = Jsoup.parse(html)
val element = doc.select("li[itemprop=something]")
val text = element.text()

output:

Text 1 Text 2 Text 3 Text 4 Text 5 Text 6

but I want them in separate lines:

Text 1
Text 2
Text 3
Text 4
Text 5
Text 6

Do you guys know how is it possible?


Solution

  • Your element object is actually an Elements object, which has a eachText() method returning a List containing the text for each of the matched elements.

    On the other hand, the text() method returns "the combined text of all the matched elements" (which has no line break as @Roland said, that's why you get all the elements on 1 line).

    So, in general you should do something like:

    doc.select("xxx").eachText().forEach(::println)