Parsing xml kotlin android

前端 未结 2 601
余生分开走
余生分开走 2021-01-19 07:17

I have xml like this:




Сегодня вас могут здорово огорчить. Если от расстройства все начнет валится из рук, просто спо         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-19 07:52

    Indeed, Simple XML Framework has a few problems with Kotlin attributes and it can be a little tricky to get things to work.

    To be honest, I am not quite sure what is the problem in your specific case, but I'd guess that the annotation shouldn't be specified for the getter, but rather for the field.

    Anyway, I am combining Simple XML and Kotlin data classes this way, and it seems to be working fine :)

    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
    )
    

    Edit: If you don't want to use data classes (which I highly recommend, but you might have a reason), this should work without the "data" keyword just fine. If you don't wish to create a constructor, just move the attribute declarations directly into the class and get rid of the @param annotation (the @field must stay).

提交回复
热议问题