XML格式化从Kotlin类

huangapple go评论50阅读模式
英文:

XML Formatting from Kotlin class

问题

我尝试从Kotlin数据类中格式化XML,并且在属性方面遇到了问题。问题是如何向一个字段添加属性,尝试格式化类似于以下内容:

<ExampleClass>
   <field actionCode="Add">aa</field>
</ExampleClass>

通过以下方式:

data class ExampleClass (
    @JacksonXmlProperty(isAttribute = true, localName = "actionCode")
    var fieldActionCode: String = "Add",

    @JacksonXmlProperty(localName = "field")
    var field: String = "aa"
)

但这样做会将属性放在错误的位置,放在Example类中:

<ExampleClass actionCode="Add">
   <field>aa</field>
</ExampleClass>

我使用FasterXML Jackson库。

英文:

I am trying to format XML from Kotlin data class and I have a problem with attributes. Question is how to add attribute to one field, trying to format something like this:

&lt;ExampleClass&gt;
   &lt;field actionCode=&quot;Add&quot;&gt;aa&lt;/field&gt;
&lt;/ExampleClass&gt;

By this:

data class ExampleClass (
    @JacksonXmlProperty(isAttribute = true, localName = &quot;actionCode&quot;)
    var fieldActionCode: String = &quot;Add&quot;,

    @JacksonXmlProperty(localName = &quot;field&quot;)
    var field: String = &quot;aa&quot;
)

But by doing so attribute goes to wrong place, to Example class:

&lt;ExampleClass actionCode=&quot;Add&quot;&gt;
   &lt;field&gt;aa&lt;/field&gt;
&lt;/ExampleClass&gt;

I use FasterXML Jackson library.

答案1

得分: 0

属性放在正确的位置,因为您将 fieldActionCode 声明为 ExampleClass 的属性,而不是其嵌套元素。

期望的输出可以通过以下方式实现:

data class ExampleClass (
    @field:JacksonXmlProperty(localName = "field")
    var field: ExampleField = ExampleField()
) {

    data class ExampleField(
        @field:JacksonXmlProperty(isAttribute = true, localName = "actionCode")
        var fieldActionCode: String = "Add",

        @field:JacksonXmlText
        var field: String = "aa"
    )
}

fun main() {
    val mapper = XmlMapper().enable(SerializationFeature.INDENT_OUTPUT)
    println(mapper.writeValueAsString(ExampleClass()))
}

产生的输出是:

<ExampleClass>
   <field actionCode="Add">aa</field>
</ExampleClass>
英文:

Attribute goes to correct place since you declare fieldActionCode as an attribute of ExampleClass, not of its nested element.

Desired output can be achieved like this:

data class ExampleClass (
    @field:JacksonXmlProperty(localName = &quot;field&quot;)
    var field: ExampleField = ExampleField()
) {

    data class ExampleField(
        @field:JacksonXmlProperty(isAttribute = true, localName = &quot;actionCode&quot;)
        var fieldActionCode: String = &quot;Add&quot;,

        @field:JacksonXmlText
        var field: String = &quot;aa&quot;
    )
}

fun main() {
    val mapper = XmlMapper().enable(SerializationFeature.INDENT_OUTPUT)
    println(mapper.writeValueAsString(ExampleClass()))
}

Produces:

&lt;ExampleClass&gt;
   &lt;field actionCode=&quot;Add&quot;&gt;aa&lt;/field&gt;
&lt;/ExampleClass&gt;

huangapple
  • 本文由 发表于 2023年2月8日 17:35:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定