首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jackson:将XML中的自定义属性反序列化为POJO

Jackson:将XML中的自定义属性反序列化为POJO
EN

Stack Overflow用户
提问于 2020-03-27 22:29:21
回答 1查看 434关注 0票数 0

我想反序列化并根据名称属性映射到值后面的类。这是我的XML文件的一部分。

代码语言:javascript
复制
                <custom-attributes>
                    <custom-attribute name="Name1" dt:dt="string">VALUE</custom-attribute>
                    <custom-attribute name="Name2" dt:dt="string"> 
                        <value>1111</value>
                        <value>1111</value>
                        <value>1111</value>
                    </custom-attribute>
                    <custom-attribute name="Name3" dt:dt="string">VALUE2</custom-attribute>
                    <custom-attribute dt:dt="boolean" name="Name3">VALUE3</custom-attribute> 
                    <custom-attribute dt:dt="boolean" name="Name4">VALUE4</custom-attribute>
                </custom-attributes>

这是我的pojo类的一部分

代码语言:javascript
复制
@JsonIgnoreProperties(ignoreUnknown = true)
public class CustomAttributes {

     @JacksonXmlProperty(localName="name3", isAttribute = true)
     private String roleID;

     public String getRoleID() {
           return roleID;
      }

     public void setRoleID(String roleID) {
          this.roleID = roleID;
}

}

你知道如何通过名称正确地读取这些属性的值吗?当前im接收null

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-28 00:22:43

我不确定结果应该是什么样子,但如果您想将完整的xml解析成匹配的对象,它们将如下所示:

代码语言:javascript
复制
public class CustomAttributeList {

    @JacksonXmlProperty(localName = "custom-attributes")
    private List<CustomAttributes> list;

    ...
}
代码语言:javascript
复制
@JacksonXmlRootElement(localName = "custom-attribute")
public class CustomAttributes {

    // the name attribute
    @JacksonXmlProperty
    private String name;

    // the datatype from the dt:dt field
    @JacksonXmlProperty(namespace = "dt")
    private String dt;

    // the content between the tags (if available)
    @JacksonXmlText
    private String content;

    // the values in the content (if available)
    @JacksonXmlProperty(localName = "value")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<String> values;

    ...
}

请注意,您问题中的localName="name3"根本没有引用属性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60888038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档