I need to unmarshal a XML string into a class that already exists in my project, but I can't figure out how to unmarshal a certain part of the XML to a list of objects. Let me explain with some code:
I have this XML:
<user>
<id>123</id>
<name>John Doe</name>
<vaddresses>
<address>
<street>Street XYZ</street>
</address>
<address>
<street>Street ABC</street>
</address>
</vaddresses>
</user>
And I have these classes:
- User
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User", propOrder = {"id", "name", "addresses"})
public class User {
@XmlElement
private int id;
@XmlElement
private String name;
@XmlElement
private List<Address> addresses;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
- Address
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Address", propOrder = {"street"})
public class Address {
@XmlElement
private String street;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
When I try to unmarshal the XML into these classes, fields id and name are processed correctly, but the same doesn't occur with the addresses:
- Output
User:
Id: 123
Nome: John Doe
Addresses: null
How do I solve this problem? Is there some JAXB annotation that I can use? And how? Or do I need to create some type of XmlAdapter? (I've tried this one but without success...)
addresses
bevadresses
in your Java code? – Claus