问题描述
我想将带有标签值的 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 erierier。但我无法用标签解决我的情况。
我也检查了这个链接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
)