首页 > 解决方案 > 具有属性 AND 值的 xml 反序列化

问题描述

我对 XML 的反序列化有点麻烦。我只能通过以下方式反序列化此 xml:

@JacksonXmlProperty(localName = "field")
@JacksonXmlElementWrapper(useWrapping = false)
List<Object> field;

这是我的xml:

   <Response>
       <user>
          <field attribute="x"></field>
          <field attribute="y">false</field>
          <field attribute="z">string</field>
       </user>
       <user>
          <field attribute="x"></field>
          <field attribute="y">false</field>
          <field attribute="z">string</field>
       </user>
   </Response>

问题是,我想用一些特定的类替换Objectin ,这样我就可以访问.List<Object> field;attributefield

有了Object,我可以创建这样的东西:

user='[{attribute=x}, {attribute=y, =false}, {name=z, =string}]

多谢。

标签: javaxmlxml-deserialization

解决方案


找到答案。我创建了包含这些元素的新类:

        @JacksonXmlProperty(isAttribute = true, localName = "attribute")
        String attribute;

        @JacksonXmlText
        String value;

Object用这个新类替换。


推荐阅读