JAXB解组xml列表元素到单个Kotlin对象

问题描述 投票:0回答:1

我有以下XML创建嵌套的Kotlin对象,但它没有映射所有TimeCycleData元素

<TimeCycle>
    <Last>
        <Date>2001-06-13T01:00:00.000Z</Date>
        <TimeCycleData>
            <Hours type="F">123</Hours>
        </TimeCycleData>
        <TimeCycleData>
            <Cycles>1234</Cycles>
        </TimeCycleData>
        <TimeCycleData>
            <Land>1234</Land>
        </TimeCycleData>
    </Last>
</TimeCycle>

并且要映射以下Kotlin数据类

data class TimeCycle(
        @field:XmlElement(name = "Last")
        val last: Last? = null
)

data class Last(

        @field:XmlElement(name = "Date")
        @field:XmlJavaTypeAdapter(value = LocalDateTimeAdapter::class, type = LocalDateTime::class)
        val date: LocalDateTime? = null,

        @field:XmlElement(name = "TimeCycleData")
        val timeCycleData: TimeCycleData? = null
)

data class TimeCycleData(

        @field:XmlElement(name = "Hours")
        val hours: DurationDetails? = null,

        @field:XmlElement(name = "Cycles")
        val cycles: Int? = null,

        @field:XmlElement(name = "Land")
        val land: Int? = null
)

data class DurationDetails(

        @field:XmlValue
        @field:XmlJavaTypeAdapter(value = DurationAdapter::class, type = Duration::class)
        val value: Duration? = null,

        @field:XmlAttribute(name = "type")
        val type: String = ""
)

当我解组XML时,仅使用TimeCycleData填充第一个Hours。如何将所有TimeCycleData合并为一个对象?

UPDATE:已更正提交的xml

java kotlin jaxb unmarshalling
1个回答
0
投票

我猜

        @field:XmlElement(name = "TimeCycleData")
        val timeCycleData: TimeCycleData? = null

应该以这种方式声明

        @field:XmlElement(name = "TimeCycleData")
        @field:XmlElementWrapper(name = "TimeCycleInfo")
        val timeCycleInfo: List<TimeCycleData>? = null

© www.soinside.com 2019 - 2024. All rights reserved.