首页 > 解决方案 > 在 kotlin 中将 xml(带标签)转换为 tikxml 时出现构建错误

问题描述

我想将带有标签值的 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 。但我无法用标签解决我的情况。

我也检查了这个链接https://github.com/Tickaroo/tikxml/blob/master/docs/AnnotatingModelClasses.md

标签: javaandroidxmlkotlintikxml

解决方案


发现了问题。

这里,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
)

推荐阅读