加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML deserialization to POJO using Jackson XmlMapper

发布时间:2020-12-16 08:50:55 所属栏目:百科 来源:网络整理
导读:Using Jackson XmlMapper annotations,how would I deserialize this XML into a pojo? ?xml version="1.0" encoding="UTF-8"? open creds priv="write" type="internal" userUsername1/user client_tokenabcplaudzrbcy37c/client_token client_secret0cxDE3L
Using Jackson XmlMapper annotations,how would I deserialize this XML into a 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>


@JacksonXmlRootElement(localName = "open")
class OpenCredentials {

    @JacksonXmlProperty(localName = "creds")
    @JacksonXmlElementWrapper(useWrapping = false)
    private Credentials[] credentials;

    //getters,setters,toString
}


class Credentials {

    @JacksonXmlProperty(isAttribute = true)
    private String priv;

    @JacksonXmlProperty(isAttribute = true)
    private String type;

    private String user;

    @JacksonXmlProperty(localName = "client_token")
    private String clientToken;

    @JacksonXmlProperty(localName = "client_secret")
    private String clientSecret;

    //getters,toString
}

Simple usage:

XmlMapper mapper = new XmlMapper();
File source = new File(xmlPath);
OpenCredentials openCredentials = mapper.readValue(source,OpenCredentials.class);
System.out.println(openCredentials);

Above program prints (for your XML):

OpenCredentials{credentials=[Credentials{priv='write',type='internal',user='Username1',client_token='abcplaudzrbcy37c',client_secret='0cxDE3LE0000='},Credentials{priv='read',client_token='123plaudzrbcy37c',client_secret='0cxDE3LE1234='},Credentials{priv='none',client_token='000plaudzrbcy37c',client_secret='0cxDE3LEabcd='}]}

See also:

  1. jackson-dataformat-xml.
  2. Home: Jackson XML databind Wiki.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读