top of page

Parsing XML RSS Feed for Android using prof18/RSS-Parser

  • agulevski10
  • Feb 21, 2022
  • 2 min read

Parsing XML response from RSS feed in Android have always been a bit complicated. All this issues are combiend in very good library RSS-Parser. As the repo says and in this example, “RSS Parser is an Android library to parse any RSS feed. With RSS Parser, you can fetch plenty of useful information from any RSS channel, be it a blog, magazine, or even a podcast feed“.


Step one: Implement as dependancy in app/build.gradle

JavaScript

dependencies { implementation 'com.prof18.rssparser:rssparser:<latest-version>' }

1

2

3

dependencies {

implementation 'com.prof18.rssparser:rssparser:<latest-version>'

}

Step two: Initialise Parser SDK:

JavaScript

val parser = Parser.Builder() .context(this) .charset(Charset.forName("ISO-8859-7")) .cacheExpirationMillis(24L * 60L * 60L * 100L) // one day .build()

1

2

3

4

5

val parser = Parser.Builder()

.context(this)

.charset(Charset.forName("ISO-8859-7"))

.cacheExpirationMillis(24L * 60L * 60L * 100L) // one day

.build()

This kind of implementation adds caching feature which can be very helpful if network is not available.

JavaScript

private fun getRssFeed() { CoroutineScope(IO).launch { initParser() } }

1

2

3

4

5

private fun getRssFeed() {

CoroutineScope(IO).launch {

initParser()

}

}

It needs to be wrap and launched with Coroutine function

JavaScript

private suspend fun initParser() { try { val channel = getParser().getChannel("https://rss.art19.com/apology-line") for (news in channel.articles) { newsList.add(News(news.image, news.author, news.content, news.description)) } //Here are your results } catch (e: Exception) { e.printStackTrace() } }

1

2

3

4

5

6

7

8

9

10

11

12

private suspend fun initParser() {

try {

val channel = getParser().getChannel("https://rss.art19.com/apology-line")

for (news in channel.articles) {

newsList.add(News(news.image, news.author, news.content, news.description))

}

//Here are your results

} catch (e: Exception) {

e.printStackTrace()

}

}

And to clear the cache just flush it:

JavaScript

parser.flushCache(url)

1

parser.flushCache(url)

And if no caching is needed just build the Parser:

JavaScript

val parser = Parser.Builder().build()

1

val parser = Parser.Builder().build()

The library can be found on this link

Comments


bottom of page