JAXB Annotated setting of a variable class that is not an element

I have an annotated JAXB class saying

@XmlRootElement(namespace = "http://www.abc.com/customer")
Class Customer{
@XmlElement(namespace = "http://www.abc.com/customer")
  private String  Name;
  @XmlElement(namespace = "http://www.abc.com/customer")
  private String  Address;
 @XmlTransient
  private HashSet set = new HashSet();

  public String getName(){
    return Name;
  }
  public void  setName(String  name){
    this.Name = name;
    set.add("Name");
  }


  public String getAddress(){
    return Address;
  }
  public void  setAddress(String  address){
    this.Address = address;
    set.add("Address");
  }

  public void getSet(){
return set;
}

I have an XML form

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Customer xmlns="http://www.abc.com/customer" >
<Name>Ralph</Name>
<Address>Newton Street</Address>
</Customer>

I use JAXB unmarshalling to get a view of the XML input object. The values ​​for the name and address are set correctly. However, the value of set is lost (since it is @XMLTransient, it is ignored)

Is there a way to ensure that it is still installed in an object that has been unarmalized? Any other annotation I can use?

+3
source share
3 answers

, . , @XmlElement getName() getAddress(), @XmlTransient getSet() .

JAXB getter/setter / , , setAddress() set, , . , getSet() , set .

+3

XmLElement . @XmlAccessType.PROPERTY , @XmlElement. XmlAccessType , JAXB , .

+2

I suggest using some post-processor utility to populate your set. Perhaps using reflection or introspection.

Note that the postprocessor will only need to check JAXB annotations, not the XML file.

0
source

Source: https://habr.com/ru/post/1739907/


All Articles