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:
- jackson-dataformat-xml.
- Home: Jackson XML databind Wiki.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…