Reputation: 1319
I'm trying to use jaxb to convert test XML to a POJO. I'm no longer getting any errors, it's just that the data is null. I was sure to add all the names to match case sensitivity and double check the tag names, but not sure why it's null.
JAXBContext jaxbContext = JAXBContext.newInstance(UserContainer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
UserContainer users = (UserContainer) jaxbUnmarshaller.unmarshal(inputFile);
Above is a snippet of code I am using to pull in the XML file from the static resources dir in Spring.
<?xml version='1.0' encoding='UTF-8'?>
<UserContainer>
<description>Test User Data for Dev Database</description>
<user>
<username>user_1</username>
<password>encrypted_password1</password>
<gender>male</gender>
<phone>4805551111</phone>
<email>[email protected]</email>
</user>
<user>
<username>user_2</username>
<password>encrypted_password2</password>
<gender>female</gender>
<phone>4805551111</phone>
<email>[email protected]</email>
</user>
<user>
<username>user_3</username>
<password>encrypted_password3</password>
<gender>trans</gender>
<phone>1113334454</phone>
<email>[email protected]</email>
</user>
</UserContainer>
import com.core.entities.User;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Allows a container to hold a list of users and map to standard POJO
*/
@XmlRootElement(name="UserContainer")
public class UserContainer {
private List<User> users;
public UserContainer() {}
public UserContainer(List<User> users) {
super();
this.users = users;
}
@XmlElement
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="user")
public class User {
@Id
@GeneratedValue
private Long id;
@XmlElement
private String username;
@XmlElement
private String password;
@XmlElement
private String gender;
@XmlElement
private String phone;
@XmlElement
private String email;
public User() {}
public User(String username, String password, String gender, String phone, String email) {
super();
this.username = username;
this.password = password;
this.gender = gender;
this.phone = phone;
this.email = email;
}
public String getUserName() {
return username;
}
public void setUserName(String username) {
this.username = username;
}
public String getEncryptedPassword() {
return password;
}
public void setEncryptedPassword(String encryptedPassword) {
this.password = encryptedPassword;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Upvotes: 0
Views: 932
Reputation:
The only change you need is add name = "user"
to @XmlElement
for getUsers()
:
@XmlElement(name = "user")
public List<User> getUsers() {
return users;
}
There are many ways:
Add @XmlElement(name = "user")
to getUsers()
or setUsers()
but not both:
@XmlRootElement(name="UserContainer")
public class UserContainer {
private List<User> users;
@XmlElement(name = "user")
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
Add @XmlElement(name = "user")
to the field users
and change @XmlAccessorType
too FIELD
@XmlRootElement(name="UserContainer")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserContainer {
@XmlElement(name = "user")
private List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
Add @XmlElement(name = "user")
to the field users
and @XmlTransient
to the public getter or setter.
@XmlRootElement(name="UserContainer")
public class UserContainer {
@XmlElement(name = "user")
private List<User> users;
@XmlTransient
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
Rename getUsers
and setUsers
into getUser
and setUser
:
@XmlRootElement(name="UserContainer")
public class UserContainer {
private List<User> users;
public List<User> getUser() {
return users;
}
public void setUser(List<User> users) {
this.users = users;
}
}
Upvotes: 0
Reputation: 1592
Make a change in your UserContainer.java file like this:
@XmlRootElement(name="UserContainer")
public class UserContainer {
@XmlElement(name="users")
private List<User> users;
public UserContainer() {}
public UserContainer(List<User> users) {
super();
this.users = users;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
Also ideally you must have your XML Element with multi values like this:
<?xml version='1.0' encoding='UTF-8'?>
<UserContainer>
<description>Test User Data for Dev Database</description>
<users>
<user>
<username>user_1</username>
<password>encrypted_password1</password>
<gender>male</gender>
<phone>4805551111</phone>
<email>[email protected]</email>
</user>
<user>
<username>user_2</username>
<password>encrypted_password2</password>
<gender>female</gender>
<phone>4805551111</phone>
<email>[email protected]</email>
</user>
<user>
<username>user_3</username>
<password>encrypted_password3</password>
<gender>trans</gender>
<phone>1113334454</phone>
<email>[email protected]</email>
</user>
</users
</UserContainer>
Upvotes: 1