JaxB: How Do I Retrieve Text Attribute from Nested Element?
up vote
0
down vote
favorite
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;
java xml jaxb
add a comment |
up vote
0
down vote
favorite
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;
java xml jaxb
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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;
java xml jaxb
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;
java xml jaxb
java xml jaxb
edited Nov 10 at 10:19
Thomas Fritsch
4,460121832
4,460121832
asked Nov 9 at 3:48
sheebs
511315
511315
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
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:
Yes that works! Thanks!
– sheebs
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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:
Yes that works! Thanks!
– sheebs
yesterday
add a comment |
up vote
1
down vote
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:
Yes that works! Thanks!
– sheebs
yesterday
add a comment |
up vote
1
down vote
up vote
1
down vote
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:
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:
edited Nov 10 at 10:30
answered Nov 10 at 10:15
Thomas Fritsch
4,460121832
4,460121832
Yes that works! Thanks!
– sheebs
yesterday
add a comment |
Yes that works! Thanks!
– sheebs
yesterday
Yes that works! Thanks!
– sheebs
yesterday
Yes that works! Thanks!
– sheebs
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219587%2fjaxb-how-do-i-retrieve-text-attribute-from-nested-element%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password