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;











share|improve this question



























    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;











    share|improve this question

























      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;











      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 10:19









      Thomas Fritsch

      4,460121832




      4,460121832










      asked Nov 9 at 3:48









      sheebs

      511315




      511315






















          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:



          enter image description here






          share|improve this answer






















          • Yes that works! Thanks!
            – sheebs
            yesterday










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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:



          enter image description here






          share|improve this answer






















          • Yes that works! Thanks!
            – sheebs
            yesterday














          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:



          enter image description here






          share|improve this answer






















          • Yes that works! Thanks!
            – sheebs
            yesterday












          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:



          enter image description here






          share|improve this answer














          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:



          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 10:30

























          answered Nov 10 at 10:15









          Thomas Fritsch

          4,460121832




          4,460121832











          • Yes that works! Thanks!
            – sheebs
            yesterday
















          • Yes that works! Thanks!
            – sheebs
            yesterday















          Yes that works! Thanks!
          – sheebs
          yesterday




          Yes that works! Thanks!
          – sheebs
          yesterday

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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














































































          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線