[英]Jackson: deserialize custom attributes in XML to POJO
I would like to deserialize and map to class following values by name attribute.我想通过名称属性反序列化并映射到以下值的类。 This is piece of my XML file.这是我的 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>
And This is piece of my pojo class这是我的 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;
}
} }
Do you know ho to properly read values from those attribues by name ?您知道如何按名称从这些属性中正确读取值吗? Currently im receiving null目前我收到空
I am not sure what the result is supposed to look like, but if you want to parse the complete xml into matching objects they would look like this:我不确定结果应该是什么样子,但是如果您想将完整的 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;
...
}
Note that the localName="name3"
from your question is not referring to a property at all.请注意,您问题中的localName="name3"
根本不是指属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.