Reputation: 129
I want the Country class to store the "ISO_3166-1_Alpha-2_Code" code. The code currently gets back the "ISO_3166-1_Numeric-3_Code" code. Can't figure out how to tweak the Country class to get the specific attribute I want.
XML:
<?xml version='1.0' encoding='UTF-8'?>
<wd:Message_Event_Configuration">
<wd:Message_Event_Configuration_Data>
<wd:Country_Reference wd:Descriptor="Saint Martin">
<wd:ID wd:type="WID">66b7082a21e510000961bb6d82b5002a</wd:ID>
<wd:ID wd:type="ISO_3166-1_Alpha-2_Code">MF</wd:ID>
<wd:ID wd:type="ISO_3166-1_Alpha-3_Code">MAF</wd:ID>
<wd:ID wd:type="ISO_3166-1_Numeric-3_Code">663</wd:ID>
</wd:Country_Reference>
<wd:Country_Reference wd:Descriptor="Saint Barthelemy">
<wd:ID wd:type="WID">881527f6cec910000ba81e8dccf61127</wd:ID>
<wd:ID wd:type="ISO_3166-1_Alpha-2_Code">BL</wd:ID>
<wd:ID wd:type="ISO_3166-1_Alpha-3_Code">BLM</wd:ID>
<wd:ID wd:type="ISO_3166-1_Numeric-3_Code">652</wd:ID>
</wd:Country_Reference>
</wd:Message_Event_Configuration_Data>
</wd:Message_Event_Configuration>
Country List:
@XmlRootElement(name = "Message_Event_Configuration")
@XmlAccessorType(XmlAccessType.FIELD)
public class Countries {
@XmlElementWrapper(name = "Message_Event_Configuration_Data")
@XmlElement(name = "Country_Reference")
private List<Country> countries = new ArrayList<Country>();
public List<Country> getCountries() {
return countries;
}
public void setCountries(List<Country> countries) {
this.countries = countries;
}
}
Country:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Country_Reference")
public class Country {
@XmlElement(name = "ID")
private String isoCode;
public Country() {
}
public Country(String isoCode) {
this.isoCode = isoCode;
}
@XmlAttribute(name = "ISO_3166-1_Alpha-2_Code")
public String getISOCode() {
return isoCode;
}
public void setISOCode(String isoCode) {
this.isoCode = isoCode;
}
}
Upvotes: 0
Views: 231
Reputation: 10147
The <Country_Reference>
XML element contains the ISO codes in a rather
sophisticated way within several <wd:ID>
XML elements.
It is therefore much too simple to model them as a Java String
property.
Instead, you need to model the Java-structure with more similarity to the XML-structure.
The sequence of XML elements <wd:ID>
can be modeled by a property List<ID> idList
which needs to be annotated by@XmlElement(name="ID")
.
The XML attribute wd:Descriptor="...."
can be modeled by a property String descriptor
which needs to be annotated by @XmlAttribute(name="Descriptor")
.
For your convenience you can add an all-arguments-constructor and some methods for getting
the WID and ISO codes from the List<ID>
.
@XmlAccessorType(XmlAccessType.FIELD)
public class Country {
@XmlAttribute(name = "Descriptor")
private String descriptor;
@XmlElement(name = "ID")
private List<ID> idList;
public Country() {
}
public Country(String descriptor, String wid, String isoAlpha2Code, String isoAlpha3Code, String isoNumeric3Code) {
this.descriptor = descriptor;
idList = new ArrayList<>();
idList.add(new ID("WID", wid));
idList.add(new ID("ISO_3166-1_Alpha-2_Code", isoAlpha2Code));
idList.add(new ID("ISO_3166-1_Alpha-3_Code", isoAlpha3Code));
idList.add(new ID("ISO_3166-1_Numeric-3_Code", isoNumeric3Code));
}
public String getWid() {
return getIdByType("WID");
}
public String getIsoAlpha2Code() {
return getIdByType("ISO_3166-1_Alpha-2_Code");
}
public String getIsoAlpha3Code() {
return getIdByType("ISO_3166-1_Alpha-3_Code");
}
public String getIsoNumeric3Code() {
return getIdByType("ISO_3166-1_Numeric-3_Code");
}
private String getIdByType(String idType) {
for (ID id : idList) {
if (id.getType().equals(idType))
return id.getValue();
}
return null;
}
}
The XML elements <wd:ID>
are quite complex. Therefore we need a separate POJO class for modeling them.
Let's call the class ID
.
The XML text between <wd:ID ..>
and </wd:ID>
is modeled by the property String value
which needs to be annotated by @XmlValue
.
The XML attribute wd:type="..."
is modeled by the property String type
which needs to be annotated by @XmlAttribute
.
For convenient use by the class Country
above, an all-arguments-constructor is added.
@XmlAccessorType(XmlAccessType.FIELD)
public class ID {
@XmlAttribute
private String type;
@XmlValue
private String value;
public ID() {
}
public ID(String type, String value) {
this.type = type;
this.value = value;
}
// public getters and setters (omitted here fro brevity)
}
The screenshot below (taken from within the debugger) visualizes the Java structure and confirms that the unmarshalling of your XML example works correctly:
Upvotes: 1