Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML deserialization to POJO using Jackson XmlMapper

Tags:

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> 

I attempted to use something like this:

@JacksonXmlRootElement(localName = "Open") public class OpenCredentials {      @JacksonXmlProperty(localName = "Credentials")     private Credentials[] credentials;  }   class Credentials {      @JacksonXmlProperty(isAttribute = true)     private String priv;      @JacksonXmlProperty(isAttribute = true)     private String type;      @JacksonXmlProperty(localName = "Creds")     private Creds[] creds; }   class Creds {      @JacksonXmlText(value = true)     private String user;      @JacksonXmlText(value = true)     private String client_token;      @JacksonXmlText(value = true)     private String client_secret; } 

When I tried to use XmlMapper's readValue(), I get the following error:

com.fasterxml.jackson.databind.JsonMappingException: Duplicate property '' for [simple type, class com.company.data.utils.api.Creds] at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:268) at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:243) 
like image 811
Bert Avatar asked Aug 28 '14 19:08

Bert


People also ask

Does Jackson parse XML?

Jackson is a library for handling JSON in Java systems and now has support for XML from version 2. DOM4J is a memory-efficient library for parsing XML, XPath, and XSLT (eXtensible Stylesheet Language).

Can ObjectMapper be used for XML?

Configuration of XML Mapper Object XmlMapper extends ObjectMapper . Therefore, you can use XML in the same way that you use ObjectMapper . For example, register the Java 8 modules to enable the feature of parameter names, Java 8 time, and Java 8 data types.


1 Answers

Your POJO classes do not fit to your XML. The structure is simpler than you thought. See below example:

@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, 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):

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:

  1. jackson-dataformat-xml.
  2. Home: Jackson XML databind Wiki.
like image 198
Michał Ziober Avatar answered Nov 12 '22 08:11

Michał Ziober