Java 使用 Jackson XmlMapper 将 XML 反序列化为 POJO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25556624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML deserialization to POJO using Hymanson XmlMapper
提问by Bert
Using Hymanson XmlMapper annotations, how would I deserialize this XML into a pojo?
使用 Hymanson XmlMapper 注释,我将如何将此 XML 反序列化为 pojo?
<?xml version="1.0" encoding="UTF-8"?>
<open>
<creds priv="write" type="internal">
<user>Username1</user>
<client_token>abcplaudzrbcy37c</client_token>
<client_secret>0cxDE3LE0000=</client_secret>
</creds>
<creds priv="read" type="internal">
<user>Username1</user>
<client_token>123plaudzrbcy37c</client_token>
<client_secret>0cxDE3LE1234=</client_secret>
</creds>
<creds priv="none" type="internal">
<user>Username1</user>
<client_token>000plaudzrbcy37c</client_token>
<client_secret>0cxDE3LEabcd=</client_secret>
</creds>
</open>
I attempted to use something like this:
我试图使用这样的东西:
@HymansonXmlRootElement(localName = "Open")
public class OpenCredentials {
@HymansonXmlProperty(localName = "Credentials")
private Credentials[] credentials;
}
class Credentials {
@HymansonXmlProperty(isAttribute = true)
private String priv;
@HymansonXmlProperty(isAttribute = true)
private String type;
@HymansonXmlProperty(localName = "Creds")
private Creds[] creds;
}
class Creds {
@HymansonXmlText(value = true)
private String user;
@HymansonXmlText(value = true)
private String client_token;
@HymansonXmlText(value = true)
private String client_secret;
}
When I tried to use XmlMapper's readValue(), I get the following error:
当我尝试使用 XmlMapper 的 readValue() 时,出现以下错误:
com.fasterxml.Hymanson.databind.JsonMappingException: Duplicate property '' for [simple type, class com.company.data.utils.api.Creds]
at com.fasterxml.Hymanson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:268)
at com.fasterxml.Hymanson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:243)
采纳答案by Micha? Ziober
Your POJO
classes do not fit to your XML
. The structure is simpler than you thought. See below example:
您的POJO
课程不适合您的XML
. 结构比你想象的要简单。见下面的例子:
@HymansonXmlRootElement(localName = "open")
class OpenCredentials {
@HymansonXmlProperty(localName = "creds")
@HymansonXmlElementWrapper(useWrapping = false)
private Credentials[] credentials;
//getters, setters, toString
}
class Credentials {
@HymansonXmlProperty(isAttribute = true)
private String priv;
@HymansonXmlProperty(isAttribute = true)
private String type;
private String user;
@HymansonXmlProperty(localName = "client_token")
private String clientToken;
@HymansonXmlProperty(localName = "client_secret")
private String clientSecret;
//getters, setters, toString
}
Simple usage:
简单用法:
XmlMapper mapper = new XmlMapper();
OpenCredentials openCredentials = mapper.readValue(XML, OpenCredentials.class);
System.out.println(openCredentials);
Above program prints (for your XML
):
以上程序打印(为您的XML
):
OpenCredentials{credentials=[Credentials{priv='write', type='internal', user='Username1', client_token='abcplaudzrbcy37c', client_secret='0cxDE3LE0000='}, Credentials{priv='read', type='internal', user='Username1', client_token='123plaudzrbcy37c', client_secret='0cxDE3LE1234='}, Credentials{priv='none', type='internal', user='Username1', client_token='000plaudzrbcy37c', client_secret='0cxDE3LEabcd='}]}
See also:
也可以看看: