为xml创建Kotlin数据类

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

有谁能帮助创建一个Kotlin数据类,以便与Retrofit和Simplexml转换器一起使用,用于这个看似简单的xml吗?

<texts>
<text id="3">
<title>Veðurhorfur á höfuðborgarsvæðinu</title>
<creation>2022-11-03 10:20:30</creation>
<valid_from>2022-11-03 12:00:00</valid_from>
<valid_to>2022-11-05 00:00:00</valid_to>
<content>Norðlæg </content>
</text>
<text id="6">
<title>Veðurhorfur</title>
<creation>2022-11-03 08:41:53</creation>
<valid_from>2022-11-05 12:00:00</valid_from>
<valid_to>2022-11-10 12:00:00</valid_to>
<content>Austan- </content>
</text>
</texts>
laximzn5

laximzn51#

基于this article,应执行以下操作:

@Root(name = "texts", strict = false)
class Texts @JvmOverloads constructor(
    @field:ElementList(inline = true)
    var textList: List<Text>? = null
)

@Root(name = "text", strict = false)
class Text @JvmOverloads constructor(
    @field:Attribute(name = "id")
    var id: String = "",
    @field:Element(name = "title")
    var title: String = "",
    @field:Element(name = "creation")
    var creation: String = "",
    @field:Element(name = "valid_from")
    var validFrom: String = "",
    @field:Element(name = "valid_to")
    var validTo: String = "",
    @field:Element(name = "content")
    var content: String = "",
)

相关问题