Problem with SimpleXML Converter语言 - PersistenceException



我尝试解析我从那里得到的xml https://www.w3schools.com/xml/simple.xml与xml SimpleXML转换器,我得到一个异常

org.simpleframework.xml.core。PersistenceException:类com.example.nbrbcurrency.retrofit.models.Food的构造函数不匹配

我POJO的

import org.simpleframework.xml.ElementList
import org.simpleframework.xml.Root
@Root(strict = false, name = "breakfast_menu")
data class BreakFastMenu @JvmOverloads constructor(
@field:ElementList(inline = true)
var foodList : List<Food>
)

import org.simpleframework.xml.Element
import org.simpleframework.xml.Root
@Root(name = "food")
data class Food @JvmOverloads constructor(
@field:Element(name = "name")
var name: String,
@field:Element(name = "price")
var price: String,
@field:Element(name = "description")
var description: String,
@field:Element(name = "calories")
var calories: String)

xml

<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>

我自己解决了。我只是预先定义了属性。

import org.simpleframework.xml.Attribute
import org.simpleframework.xml.ElementList
import org.simpleframework.xml.Root
@Root(strict = false, name = "DailyExRates")
data class CurrencyDataList constructor(
@field:Attribute(name = "Date")
var date: String = "",
@field:ElementList(inline = true)
var currencies: List<CurrencyData> = ArrayList()
)