Reputation: 157
I have a Java class which contains a List field.
public class Machine{
public int ID;
public String Name;
public String Status;
public String Environment;
public String Cluster;
public List<String> SupportedLocales;
}
Below is an instance of the class in XML that I am trying to unmarshall.
<?xml version="1.0" encoding="utf-8"?>
<Machine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>27</ID>
<Name>QA14ADMINOPM201</Name>
<Status>Decom</Status>
<Environment>QA14</Environment>
<Cluster>ADMINOPM</Cluster>
<SupportedLocales>
<SupportedLocale>NA</SupportedLocale>
<SupportedLocale>Global</SupportedLocale>
</SupportedLocales>
</Machine>
When I unmarshall the Xml though, the resulting object has an empty SupportedLocales
list, with no elements. Any reason why this might be happening or suggested fixes? All other fields are being unmarshalled correctly.
Upvotes: 3
Views: 644
Reputation: 72854
Annotate your field with an XmlElementWrapper
annotation:
@XmlElementWrapper(name = "SupportedLocales")
@XmlElement(name = "SupportedLocale")
public List<String> SupportedLocales;
Also try to follow the convention of using variables that start with a lowercase (supportedLocales
instead of SupportedLocales
). This would need to map the XML element name for each field as follows:
@XmlElement(name="ID")
public int id;
@XmlElement(name="Name")
public String name;
@XmlElement(name="Status")
public String status;
@XmlElement(name="Environment")
public String environment;
@XmlElement(name="Cluster")
public String cluster;
@XmlElementWrapper(name = "SupportedLocales")
@XmlElement(name = "SupportedLocale")
public List<String> supportedLocales;
Upvotes: 4