首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Kotlin Retrofit + SimpleXMLConverter ElementList解析不正确

Android Kotlin Retrofit + SimpleXMLConverter ElementList解析不正确
EN

Stack Overflow用户
提问于 2018-04-26 05:11:46
回答 1查看 1.2K关注 0票数 3

我正在尝试解析一些podcast xml提要,但无法获得所有类别和具有相同名称的其他多个字段。

提要示例:http://demo3984434.mockable.io/cast

代码语言:javascript
复制
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:itunesu="http://www.itunesu.com/feed" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        <title>Podcast Title</title>
        <link>http://link.com</link>
        <language>en-us</language>
        <itunes:subtitle>Subtitle!!!</itunes:subtitle>
        <itunes:author>Author Name</itunes:author>
        <media:category scheme="http://www.itunes.com/dtds/podcast-1.0.dtd">Comedy</media:category>
        <media:category scheme="http://www.itunes.com/dtds/podcast-1.0.dtd">Society &amp; Culture</media:category>
        <media:category scheme="http://www.itunes.com/dtds/podcast-1.0.dtd">Games &amp; Hobbies/Video Games</media:category>
        <media:category scheme="http://www.itunes.com/dtds/podcast-1.0.dtd">TV &amp; Film</media:category>
        <media:category scheme="http://www.itunes.com/dtds/podcast-1.0.dtd">Music</media:category>
        <itunes:category text="Comedy"/>
        <itunes:category text="Society &amp; Culture"/>
        <itunes:category text="Games &amp; Hobbies"/>
        <itunes:category text="Video Games"/>
        <itunes:category text="TV &amp; Film"/>
        <itunes:category text="Music"/>
    </channel>
</rss>

RSSFeed.kt:

代码语言:javascript
复制
@Root(name = "rss", strict = false)
class RSSFeed {

    @field:Element(name = "channel")
    var mFeedChannel: FeedChannel? = null
}

FeedChannel.kt

代码语言:javascript
复制
@Root(name = "channel", strict = false)
open class FeedChannel {

    @field:Element(name = "title")
    var mTitle: String? = null

    @field:Element(name = "link")
    var mLink: String? = null

    @field:Element(name = "subtitle")
    var mSubtitle: String? = null

    @field:Element(name = "author")
    var mAuthor: String? = null

    @field:ElementList(entry = "category", inline = true)
    var mCategories: List<Category>? = null

    @Root(name = "category", strict = false)
    class Category {

        @field:Element(name = "category")
        var category: String? = null
    }
}

但类别列表元素始终为空:

EN

回答 1

Stack Overflow用户

发布于 2018-04-26 14:59:40

找到解决方案:

更改了@field:Element字段,并使用String代替另一个类Category。感谢@DaveThomas

代码语言:javascript
复制
@Root(name = "channel", strict = false)
class FeedChannel {

    @field:Element(name = "title")
    var mTitle: String? = null

    @field:Element(name = "link")
    var mLink: String? = null

    @field:Element(name = "subtitle")
    var mSubtitle: String? = null

    @field:Element(name = "author")
    var mAuthor: String? = null

    @field:ElementList(entry = "category", type = String::class, inline = true)
    var mCategories: List<String>? = null

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50031409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档