【发布时间】:2018-10-06 11:16:34
【问题描述】:
我正在尝试解析一些播客 xml 提要,但无法获取所有类别和其他具有相同名称的多个字段。
Feed 示例:http://demo3984434.mockable.io/cast
<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 & Culture</media:category>
<media:category scheme="http://www.itunes.com/dtds/podcast-1.0.dtd">Games & Hobbies/Video Games</media:category>
<media:category scheme="http://www.itunes.com/dtds/podcast-1.0.dtd">TV & 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 & Culture"/>
<itunes:category text="Games & Hobbies"/>
<itunes:category text="Video Games"/>
<itunes:category text="TV & Film"/>
<itunes:category text="Music"/>
</channel>
</rss>
RSSFeed.kt:
@Root(name = "rss", strict = false)
class RSSFeed {
@field:Element(name = "channel")
var mFeedChannel: FeedChannel? = null
}
FeedChannel.kt
@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
}
}
【问题讨论】:
-
var mCategories: List<String>? = null可能适用于前 5 个 media:category 元素 -
在 Category 类定义中查看您是如何说一个类别中有一个类别的。
<category> <category/> </category> -
@DaveThomas 现在我得到了这个异常:org.simpleframework.xml.core.ValueRequiredException: Unable to meet @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline =true, name=category, required=true, type=void) on field 'mCategories' private java.util.List ....model.FeedChannel.mCategories for class ....model.FeedChannel 在第 5 行
-
看起来它对那些具有属性值的元素感到窒息。旁注:您可以更新您的问题以包含说明您正在使用 org.simpleframework.xml 的信息。熟悉该框架的人会发现您的问题并帮助您找到解决方案。
-
@DaveThomas 我找到了一个基于你所说的解决方案,只是将注释字段从“名称”更改为“条目”并添加“类型”字段
标签: android xml kotlin retrofit2 simple-framework