Quantcast
Channel: Deserialize XML starting with child element using Jackson - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Deserialize XML starting with child element using Jackson

$
0
0

I am attempting to parse some XML using Jackson. However, I can't figure out how to start with the element when parsing. This API call will never return more than one :

<return_data api_call="get"><users><user><username>jdoe</username><firstname>John</firstname><lastname>Doe</lastname><account_expiration_date>0000-00-00</account_expiration_date><signup_date>2015-01-01 15:00:36</signup_date><deleted>0</deleted><account_name>my account</account_name></user></users></return_data>

I tried the following, but am getting the error 'Unrecognized field "users"... not marked as ignorable'.

@JacksonXmlRootElement(localName = "user")@JsonIgnoreProperties(value = { "api_call" })public class User {@JsonProperty("username")private String userName;@JsonProperty("firstname")private String firstName;@JsonProperty("lastname")private String lastName;@JsonProperty("account_expiration_date")private Date expirationDate;@JsonProperty("deleted")private boolean deleted;@JsonProperty("signup_date")private Date signupDate;public String getUserName() {    return userName;}public void setUserName(String userName) {    this.userName = userName;}public String getFirstName() {    return firstName;}public void setFirstName(String firstName) {    this.firstName = firstName;}public String getLastName() {    return lastName;}public void setLastName(String lastName) {    this.lastName = lastName;}public Date getExpirationDate() {    return expirationDate;}public void setExpirationDate(Date expirationDate) {    this.expirationDate = expirationDate;}public Date getSignupDate() {    return signupDate;}public void setSignupDate(Date signupDate) {    this.signupDate = signupDate;}}

Update:I was able to get it working by using multiple wrappers, but I'm hoping there is an easier solution.

@JsonIgnoreProperties(value = { "api_call" })public class ReturnDataWrapper {@JacksonXmlElementWrapper(localName = "users")@JsonProperty("users")private UserWrapper userWrapper;public UserWrapper getUserWrapper() {    return userWrapper;}public void setUserWrapper(UserWrapper userWrapper) {    this.userWrapper = userWrapper;}}@JacksonXmlRootElement(localName = "users")@JsonRootName("users")public class AlertAccountWrapper {@JacksonXmlElementWrapper(localName = "user")@JsonProperty("user")public User account;public User getUser() {    return account;}public void setUser(User account) {    this.account = account;}}

I also removed @JacksonXmlRootElement(localName = "user") and @JsonIgnoreProperties(value = { "api_call" }) from the User class.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images