繁体   English   中英

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

[英]Jackson: deserialize custom attributes in XML to POJO

我想通过名称属性反序列化并映射到以下值的类。 这是我的 XML 文件的一部分。

                <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 课的一部分

@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;
}

}

您知道如何按名称从这些属性中正确读取值吗? 目前我收到空

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

public class CustomAttributeList {

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

    ...
}
@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"根本不是指属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM