解析xml kotlin android [英] Parsing xml kotlin android
问题描述
我有这样的xml:
<horo>
<aries>
<today>
Сегодня вас могут здорово огорчить. Если от расстройства все начнет валится из рук, просто спокойно сядьте и тихонько подождите хорошей новости.
</today>
</aries>
<taurus>
<today>
Сегодня у вас могут возникнуть проблемы на личном фронте. Спасти вас от перспективы оказаться не у дел может сухой, рациональный и в высшей степени объективный подход к проблеме.
</today>
</taurus>
</horo>
现在我学习kotlin whith改装。我包含解析xml的库,而不是我不能理解如何解析这个xml的create对象。我有对象:
And now i learning kotlin whith retrofit. I include libraries for parse xml, and not i cant understand how create object for parsing this xml. I have object:
@Root(name = "horo", strict = false)
open class DailyHoroscope{
@get : Element(name = "aries") var aries : Aries? = null
}
@Root(name = "aries", strict = false)
open class Aries{
@get : Element(name = "today") var today : String? = null
}
但我有错误:
rg.simpleframework.xml.core.ConstructorException:默认构造函数
不能接受只读@ org.simpleframework.xml.Element(data = false,
name = aries,required = true,type = void)类
ac.kotlintest.model中方法'aries'。
rg.simpleframework.xml.core.ConstructorException: Default constructor can not accept read only @org.simpleframework.xml.Element(data=false, name=aries, required=true, type=void) on method 'aries' in class ac.kotlintest.model.
更新
我在java中写了代码:
i writed code in java:
@Root(name = "horo", strict = false)
public class DailyHoroscopeJ {
@Element(name = "aries")
public Aries aries;
public Aries getAries() {
return aries;
}
public void setAries(Aries aries) {
this.aries = aries;
}
}
@Root(name = "aries", strict = false)
class Aries{
@Element(name = "today")
public String today;
public String getToday() {
return today;
}
public void setToday(String today) {
this.today = today;
}
}
它工作正常,然后我转换为kotlin
and it work fine, then i convert to kotlin
@Root(name = "horo", strict = false)
class DailyHoroscope {
@get:Element(name = "aries")
var aries:Aries? = null
}
@Root(name = "aries", strict = false) class Aries {
@get:Element(name = "today")
var today:String? = null
}
但我有同样的问题((((p>
but i have same problem((((
推荐答案
@ daementus的答案几乎是完美的。如果你想使用默认参数的构造函数注入,你必须强制Kotlin生成构造函数重载:
The answer of @daementus is almost perfect. If you want to use constructor injection with default parameters, you have to force Kotlin to generate constructor overloads:
data class Section @JvmOverloads constructor(
@field:Element(name = "id")
@param:Element(name = "id")
val id: Long,
@field:Attribute(name = "title", required = false)
@param:Attribute(name = "title", required = false)
val title: String = ""
)
如果没有它,你将得到构造函数与类Section 不匹配。
默认情况下,Kotlin会生成一个包含所有参数和特殊构造函数的构造函数。
Without it you will get Constructor not matched for class Section. By default Kotlin generates a constructor with all parameters and a special constructor.
注意:我更愿意在评论中回答,但我没有足够的积分。
这篇关于解析xml kotlin android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!