Skip to content
IvanYH Wu edited this page Jul 23, 2023 · 13 revisions

Download

First, you can add this repository to the root of your project build.gradle file under the allprojects.

allprojects {
  repositories {
   ...
   maven { url 'https://jitpack.io' }
  }
}

or add Jitpack in settings.gradle

ependencyResolutionManagement {
    ...
    repositories {
        ...
        maven { url 'https://jitpack.io' } // Add this line
    }
}

Then, add this dependency to the build.gradle file in app directory.

dependencies {
    implementation "com.github.ivanisidrowu.KtRssReader:kotlin:v2.2.1"
}

If you want to customize data format, you have to add these dependencies.

apply plugin: 'kotlin-kapt'

dependencies {
    implementation "com.github.ivanisidrowu.KtRssReader:kotlin:v2.2.1"
    implementation "com.github.ivanisidrowu.KtRssReader:annotation:v2.2.1"
    kapt "com.github.ivanisidrowu.KtRssReader:processor:v2.2.1"
}

Data Models

Before we get into the basic API usage, let's talk about the data models in KtRssReader. KtRssReader provides 4 model classes and custom annotations for you to get different kinds of tags you need. You can check on Data Models.

General Data Parsers

We provide 4 general parsers, RssStandardParser, GoogleParser, ITunesParser, and AutoMixParser. Those 4 parsers will output the data models we mentioned.

For example,

val rssModel: RssStandardChannel = RssStandardParser().parse(xmlContet)
val googleModel: GoogleChannel = GoogleParser().parse(xmlContent)
val itunesModel: ITunesChannel = ITunesParser().parse(xmlContent)
val model: AutoMixChannel = AutoMixParser().parse(xmlContent)

Custom Data Parsers

Custom Data

In KtRssReader, we provide annotations to let you define custom models. Don't forget to rebuild the project after defining custom data classes with annotations! This is REALLY important.

Setup

To enable Kotlin parser generator, you must add the following kapt arguments in your build.gradle file.

kapt {
    arguments {
        arg("pureKotlinParser", true)
    }
}

@RssTag

Params:

  • name: The tag name in String.
  • order: Custom parsing order array. The type in the array is OrderType enum. 3 types are available, RSS_STANDARD, ITUNES, GOOGLE. The default order array is [OrderType.RSS_STANDARD, OrderType.ITUNES, OrderType.GOOGLE]. When parsing the RSS feed, the parser will follow the order to find the available value for the property. Take the default order array as an example, the parser will check the RSS standard tag value first, if it has a value, it will not check the iTunes tag and put the RSS standard tag value to the custom model. Otherwise, it will continue to find an available value.

Class example:

@RssTag(name = "channel")
data class MyChannel(
    val title: String?,
    @RssTag(name = "author", order = [OrderType.ITUNES, OrderType.RSS_STANDARD])
    val name: String?,
): Serializable

Data example:

<channel>
    <title>the title</title>
    <author>the author</author>
    <itunes:author>itunes author</itunes:author>
</channel>

@RssAttribute

Params:

  • name: The name of the attribute.

Class example:

@RssTag(name = "category")
data class Category(
    @RssAttribute(name = "title")
    val categoryTitle: String?,
): Serializable

Data example:

<category title = "the title">this is a category</category>

The value of the categoryTitle will be "the title".

@RssValue

Get the value inside a tag.

Class example:

@RssTag(name = "category")
data class Category(
    @RssValue
    val value: String?,
    @RssAttribute
    val title: String?,
): Serializable

Data example:

<category title = "the title">this is a category</category>

In this case, the property value of the data class will be "this is a category".

@RssRawData

An annotation to let you parse tags with specific raw data in RSS feed.

Params:

  • rawTags: It's an array that contains tag names you would like to parse, and the parser will follow the order of the array to find tag candidates.

Class example:

@RssTag(name = "channel")
data class RawRssData(
    @RssRawData(rawTags = ["googleplay:author", "itunes:author"])
    val author: String?,
    val title: String?,
): Serializable

Data example:

<channel>
    <itunes:author>itunes author</itunes:author>
    <titile>title</title>
</channel>

The parsing result of the author, in this case, will be "itunes author" because the tag <googleplay:author> does not exist in the data. So the parser uses the backup tag <itunes:author> defined in @RssRawData.

Samples

You can check out our tests as samples.