无法使用retofit解析对Kotlin数据类的xml响应

axzmvihb  于 2022-11-16  发布在  Kotlin
关注(0)|答案(1)|浏览(119)

我正在尝试调用给予xml响应的rest api。我正在使用带有simplexml转换器工厂的retrofit将xml解析为Kotlin数据类。下面是来自api的xml响应:

<category>
    <api-version>1.0</api-version>
    <books>
        <list>
            <result>
                <author>Hello1</author>
                <brief>Hii</brief>
            </result>
            <result>
                <author>Hello2</author>
                <brief>Hii2</brief>
            </result>
        </list>
    </books>
</category>

下面是我的Kotlin数据类

data class CategoryResponseModel(
        @field:Element(name = "api-version", required = false)
        @param:Element(name = "api-version", required = false)
        val apiVersion: String? = null,

        @field:Element(name = "books", required = false)
        @param:Element(name = "books", required = false)
        val books: Books? = null
)

data class Books(
        @field:Element(name = "list", required = false)
        @param:Element(name = "list", required = false)
        val list: BookList? = null
)

@Root(name = "list")
data class BookList(
        @field:Element(name = "result", required = false)
        @param:Element(name = "result", required = false)
        val result: List<BookSearchResult>? = null,
)

data class BookSearchResult(

        @field:Element(name = "author", required = false)
        @param:Element(name = "author", required = false)
        val author: String? = null,

        @field:Element(name = "brief", required = false)
        @param:Element(name = "brief", required = false)
        val briefSynopsis: String? = null,
)

但是,在对org.simpleframework.xml.core.ElementException: Element 'result[1]/author[1]' does not have a match in class BookList的SimpleXML进行改型时出现错误

2q5ifsrm

2q5ifsrm1#

经过这么多的发现和尝试和错误,我得到了解决方案。
我只是在块下面的数组列表元素中做了更改,

@Root(name = "list")
data class BookList(
        @field:ElementList(inline = true, required = false)
        @param:ElementList(inline = true, required = false)
        val result: List<BookSearchResult>? = null,
)

完整解决方案代码如下。

data class CategoryResponseModel(
        @field:Element(name = "api-version", required = false)
        @param:Element(name = "api-version", required = false)
        val apiVersion: String? = null,

        @field:Element(name = "books", required = false)
        @param:Element(name = "books", required = false)
        val books: Books? = null
)

data class Books(
        @field:Element(name = "list", required = false)
        @param:Element(name = "list", required = false)
        val list: BookList? = null
)

@Root(name = "list")
data class BookList(
        @field:ElementList(inline = true, required = false)
        @param:ElementList(inline = true, required = false)
        val result: List<BookSearchResult>? = null,
)

data class BookSearchResult(

        @field:Element(name = "author", required = false)
        @param:Element(name = "author", required = false)
        val author: String? = null,

        @field:Element(name = "brief", required = false)
        @param:Element(name = "brief", required = false)
        val briefSynopsis: String? = null,
)

相关问题