【问题标题】:Kotlin - parsing xml inline list with simpleXMLKotlin - 使用 simpleXML 解析 xml 内联列表
【发布时间】:2018-07-14 08:39:06
【问题描述】:

我正在尝试在 kotlin 中使用 simplexml 解析 rss 提要。

提要是top podcasts feed from itunes

返回的数据有这样的结构:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Top Audio Podcasts</title>
    <link>https://rss.itunes.apple.com/api/v1/us/podcasts/top-podcasts/all/10/explicit.rss</link>
    <lastBuildDate>Fri, 13 Jul 2018 01:36:19 -0700</lastBuildDate>
    <atom:link rel="self" type="application/rss+xml" href="https://rss.itunes.apple.com/api/v1/us/podcasts/top-podcasts/all/10/explicit.rss"/>
    <atom:link rel="alternate" type="text/html" href="https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?genreId=26&amp;popId=28"/>
    <description>iTunes Store : Top Audio Podcasts</description>
    <category>iTunes Store</category>
    <copyright>Copyright © 2018 Apple Inc. All rights reserved.</copyright>
    <item>
      <title>The Wilderness - Crooked Media</title>
      <category domain="">Crooked Media</category>
      <link>https://itunes.apple.com/us/podcast/the-wilderness/id1408796715?mt=2</link>
      <guid>https://itunes.apple.com/us/podcast/the-wilderness/id1408796715?mt=2</guid>
      <description>The Wilderness</description>
      <category>podcast</category>
      <category>News &amp; Politics</category>
      <category>Podcasts</category>
      <category>Society &amp; Culture</category>
      <category>History</category>
      <pubDate>Fri, 6 Jul 2018 00:00:00 +0000</pubDate>
    </item>
    <item>
      <title>Aaron Mahnke's Cabinet of Curiosities - HowStuffWorks &amp; Aaron Mahnke</title>
      <category domain="https://itunes.apple.com/us/artist/howstuffworks-aaron-mahnke/284341002?mt=2">HowStuffWorks &amp; Aaron Mahnke</category>
      <link>https://itunes.apple.com/us/podcast/aaron-mahnkes-cabinet-of-curiosities/id1396546917?mt=2</link>
      <guid>https://itunes.apple.com/us/podcast/aaron-mahnkes-cabinet-of-curiosities/id1396546917?mt=2</guid>
      <description>Aaron Mahnke's Cabinet of Curiosities</description>
      <category>podcast</category>
      <category>History</category>
      <category>Podcasts</category>
      <category>Society &amp; Culture</category>
      <pubDate>Tue, 10 Jul 2018 00:00:00 +0000</pubDate>
    </item>
  </channel>
</rss>

我关心的部分是项目标签所包含的播客列表。

我拥有的模型类如下所示:

@Root(name = "channel", strict = false)
data class Chart @JvmOverloads constructor(
        @field:ElementList(entry = "item", inline = true)
        @param:ElementList(entry = "item", inline = true)
        val podcasts: List<Entry>? = null)

@Root(name = "item", strict = false)
data class Entry @JvmOverloads constructor(
        @field:Element(name = "description")
        @param:Element(name = "description")
        val title: String? = null,

        @field:Element(name = "category")
        @param:Element(name = "category")
        val artist: String? = null,

        @field:Element(name = "guid")
        @param:Element(name = "guid")
        val guid: String? = null)

我有一个简单的单元测试,它从文件加载 xml,将其传递给 simplexml 反序列化器,然后将输出与一些预期的模型进行比较。

在我运行测试的那一刻,我得到一个异常:

org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(entry=item, inline=true, data=false, name=, type=void, required=true, empty=true) on field 'podcasts' private final java.util.List Chart.podcasts for class Chart at line 2

在 @ElementList 注释中添加 required = false 会消除此错误,但反序列化器会为列表返回 null。

我最好的猜测是反序列化器找不到项目列表,尽管根据我对@ElementList 的理解,使用输入字段应该允许它找到标签。

此代码的用例是形成依赖 Retrofit2 进行 REST 调用的 Android 应用的网络层。

感谢您对此提供的任何帮助。

注意:@Param 和 @Field 声明的使用是为了应对不同的异常,I got the idea from this question

【问题讨论】:

    标签: android kotlin retrofit2 simple-framework


    【解决方案1】:

    我找到了解决办法。

    所以第一个问题是我的根元素不是表示频道标签的图表,我需要一个包装类,我称之为 ChartFeed 来表示 rss 标签。

    我通过在 Chart 对象上为标题标签添加一个元素来实现这一点,这也是无法找到的。

    我完成的数据类如下所示:

    @Root(name = "rss", strict = false)
    data class ChartFeed(
            @field:Element(name = "channel")
            @param:Element(name = "channel")
            val chart: Chart? = null)
    
    @Root(name = "channel", strict = false)
    data class Chart @JvmOverloads constructor(
            @field:ElementList(entry = "item", inline = true)
            @param:ElementList(entry = "item", inline = true)
            val podcasts: List<Entry>? = null)
    
    @Root(name = "item", strict = false)
    data class Entry @JvmOverloads constructor(
            @field:Path("description")
            @field:Text(required = false)
            @param:Path("description")
            @param:Text(required = false)
            val title: String? = null,
    
            @field:Path("category")
            @field:Text(required = false)
            @param:Path("category")
            @param:Text(required = false)
            val artist: String? = null,
    
            @field:Element(name = "guid")
            @param:Element(name = "guid")
            val guid: String? = null)
    

    我已将一些字段/参数的@Element 标记更改为@Path 和@Text 的组合,这是因为通道和项目 xml 对象都重用了导致此异常的相同标记:

    org.simpleframework.xml.core.PersistenceException: Element 'category' is already used with @org.simpleframework.xml.Element(name=category, type=void, data=false, required=true) on field 'artist' private final java.lang.String Entry.artist at line 18
    

    我使用的解决方案详解here

    【讨论】:

    • 这是一个很大的帮助!我遇到的另一个奇怪的问题是,如果我在某些成员上不存在的数组成员上缺少“required=false”声明,那么它将失败而不会返回有意义的异常。
    猜你喜欢
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多