Jackson:将 XML 中的自定义属性反序列化为 POJO
Jackson: deserialize custom attributes in XML to POJO
我想反序列化并映射到 class 以下值的名称属性。
这是我的 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 class
@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;
}
}
您知道如何通过名称从这些属性中正确读取值吗?目前我收到 null
我不确定结果应该是什么样子,但如果你想要
将完整的 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"
根本不是指 属性。
我想反序列化并映射到 class 以下值的名称属性。 这是我的 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 class
@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;
}
}
您知道如何通过名称从这些属性中正确读取值吗?目前我收到 null
我不确定结果应该是什么样子,但如果你想要 将完整的 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"
根本不是指 属性。