SimpleXML:反序列化项目列表

SimpleXML: deserializing a list of items

我有这个 XML 使用简单 XML

反序列化
<Content>
    <Items count="2">
        <Device>
            <User>
                <Lastname>Straw</Lastname>
                <Firstname>Jim</Firstname>
            </User>
        </Device>
        <Device>
            <User>
                <Lastname>Straw</Lastname>
                <Firstname>John</Firstname>
            </User>
        </Device>
    </Items>
</Content>

我用 Kotlin 制作了这个 POJO,但它与 XML

不匹配
@Root
data class Content(@field:ElementList(entry="Device") var device: List<Device>)

@Root
data class Device(@field:Element(name="User") var user: User? = null, 
    @field:Element(name="InvDevice") var invDevice: InvDevice? = null)

@Root
data class User(@field:Element(name="Lastname") var lastname: String? = null,
    @field:Element(name="Firstname") var firstname: String? = null)

我有一个 ElementException:元素 'Items' 在 class

中没有匹配项

按照推荐使用 Android Studio 中的 POJO Generator 插件 ,我找到了如何制作我的数据 类

@Root
data class Content(@field:Element(name = "Items", required = false) var items: Items? = null)

data class Items(
    @field:ElementList(name = "Device", required = false, entry = "Device", inline = true)
    var deviceList: List<Device>? = null,
    @field:Attribute(name="count", required = false)
    var count: Int? = null
)

data class Device(@field:Element(name="User", required = false) var user: User? = null)

data class User(@field:Element(name="Lastname", required = false) var lastname: String? = null,
                @field:Element(name="Firstname", required = false) var firstname: String? = null)