解析 xml kotlin android
Parsing xml kotlin android
我有 xml 这样的:
<horo>
<aries>
<today>
Сегодня вас могут здорово огорчить. Если от расстройства все начнет валится из рук, просто спокойно сядьте и тихонько подождите хорошей новости.
</today>
</aries>
<taurus>
<today>
Сегодня у вас могут возникнуть проблемы на личном фронте. Спасти вас от перспективы оказаться не у дел может сухой, рациональный и в высшей степени объективный подход к проблеме.
</today>
</taurus>
</horo>
现在我正在学习 kotlin 和改造。我包含用于解析 xml 的库,但我无法理解如何创建用于解析此 xml 的对象。我有对象:
@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: 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.
upd
我在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
@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
}
但我有同样的问题((((
事实上,Simple XML Framework 在 Kotlin 属性方面存在一些问题,要让它正常工作可能有点棘手。
老实说,我不太确定您的具体情况有什么问题,但我猜不应该为 getter 指定注释,而应该为字段指定注释。
无论如何,我正在以这种方式组合简单 XML 和 Kotlin 数据 classes,它似乎工作正常:)
data class Section (
@field:Element(name = "id", required = false)
@param:Element(name = "id", required = false)
val id: Long? = null,
@field:Attribute(name = "title", required = false)
@param:Attribute(name = "title", required = false)
val title: String? = null
)
编辑:如果您不想使用 data classes(我强烈推荐,但您可能有理由),这应该可以在没有 "data" 关键字的情况下正常工作。如果您不想创建构造函数,只需将属性声明直接移动到 class 中并去掉 @param 注释(@field 必须保留)。
@daementus 的回答近乎完美。如果要使用带默认参数的构造函数注入,则必须强制 Kotlin 生成构造函数重载:
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 = ""
)
没有它,您将得到 Constructor not matched for class Section。
默认情况下,Kotlin 会生成一个包含所有参数的构造函数和一个特殊的构造函数。
注:我想在评论里回答,但是积分不够
我有 xml 这样的:
<horo>
<aries>
<today>
Сегодня вас могут здорово огорчить. Если от расстройства все начнет валится из рук, просто спокойно сядьте и тихонько подождите хорошей новости.
</today>
</aries>
<taurus>
<today>
Сегодня у вас могут возникнуть проблемы на личном фронте. Спасти вас от перспективы оказаться не у дел может сухой, рациональный и в высшей степени объективный подход к проблеме.
</today>
</taurus>
</horo>
现在我正在学习 kotlin 和改造。我包含用于解析 xml 的库,但我无法理解如何创建用于解析此 xml 的对象。我有对象:
@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: 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.
upd
我在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
@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
}
但我有同样的问题((((
事实上,Simple XML Framework 在 Kotlin 属性方面存在一些问题,要让它正常工作可能有点棘手。
老实说,我不太确定您的具体情况有什么问题,但我猜不应该为 getter 指定注释,而应该为字段指定注释。
无论如何,我正在以这种方式组合简单 XML 和 Kotlin 数据 classes,它似乎工作正常:)
data class Section (
@field:Element(name = "id", required = false)
@param:Element(name = "id", required = false)
val id: Long? = null,
@field:Attribute(name = "title", required = false)
@param:Attribute(name = "title", required = false)
val title: String? = null
)
编辑:如果您不想使用 data classes(我强烈推荐,但您可能有理由),这应该可以在没有 "data" 关键字的情况下正常工作。如果您不想创建构造函数,只需将属性声明直接移动到 class 中并去掉 @param 注释(@field 必须保留)。
@daementus 的回答近乎完美。如果要使用带默认参数的构造函数注入,则必须强制 Kotlin 生成构造函数重载:
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 = ""
)
没有它,您将得到 Constructor not matched for class Section。 默认情况下,Kotlin 会生成一个包含所有参数的构造函数和一个特殊的构造函数。
注:我想在评论里回答,但是积分不够