我想将带有标记值的xml转换为tikxml。但失败了。
需要转换的XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<resCreateField>
<countryCode mandatory="1">1</countryCode>
<birthDate mandatory="1">1</birthDate>
<policies>
<skipEmailVerification>N</skipEmailVerification>
<require2FactorConfiguration mandatory="1">Y</require2FactorConfiguration>
</policies>
</resCreateField>
我的方法:
@Xml(name = "countryCode")
data class CountryCode(
@PropertyElement(name = "countryCode")
val isCountryCodeMandatory: String? = null,
@Attribute(name = "mandatory")
val countryCodeMandatoryValue: String? = null
)
@Xml(name = "birthDate")
data class BirthDate(
@Attribute(name = "mandatory")
val birthDateMandatoryValue: String? = null,
@PropertyElement(name = "birthDate")
val isBirthDateMandatory: String? = null
)
@Xml(name = "require2FactorConfiguration")
data class Require2FactorConfiguration(
@PropertyElement(name = "require2FactorConfiguration")
val isEmailReceiveYNFlagMandatory: String? = null,
@Attribute(name = "mandatory")
val emailReceiveYNFlagMandatoryValue: String? = null
)
@Xml(name = "policies")
data class Policies(
@PropertyElement(name = "skipEmailVerification")
val skipEmailVerification: String? = null,
@Element(name = "require2FactorConfiguration")
val require2FactorConfiguration: Require2FactorConfiguration
)
@Xml(name = "resCreateField")
data class FieldInfoResponse(
@Element(name = "countryCode")
val countryCode: CountryCode?,
@Element(name = "birthDate")
val birthDate: BirthDate?,
@Element(name = "policies")
val policies: Policies?
)
但我得到了以下错误。我找不出原因。
error: The constructor parameter 'isBirthDateMandatory' in constructor
BirthDate(java.lang.String,java.lang.String) in class com.example.utils.server.response.BirthDate is annotated with a TikXml annotation. Therefore a getter method with minimum package visibility with the name getIsBirthDateMandatory() or isBirthDateMandatory() in case of a boolean must be provided. Unfortunately, there is no such getter method. Please provide one!
java.lang.String isBirthDateMandatory)
对于那些没有标记的xml,我使用了tikxml-eralier。但是我不能用标签来解决我的案子。
我也检查了这个链接https://github.com/Tickaroo/tikxml/blob/master/docs/AnnotatingModelClasses.md
发现问题。
这里,countryCode 1的值是一个描述。
<countryCode mandatory="1">1</countryCode>
因此,xml应该以这种方式编写
@Xml(name = "countryCode")
data class CountryCode(
@TextContent
val value: String? = null,
@Attribute(name = "mandatory")
val countryCodeMandatoryValue: String? = null
)