2017-03-28 98 views
0

我的枚举类型ProductType正确保存到XML,但它不想在打开文件时解组。Java JAXB反编组枚举

我做EnumAdapter:

public class EnumAdapter extends XmlAdapter<String, ProductType> 
{ 
    @Override 
    public ProductType unmarshal(String value) throws Exception { 
     try { 
      return ProductType.valueOf(value); 
     } 
     catch(Exception e) { 
      throw new JAXBException(e); 
     } 
    } 

    @Override 
    public String marshal(ProductType value) { 
     return value.toString(); 
    } 

} 

我的产品类:

public class Product { 

    private final IntegerProperty ilosc; //quantity 
    private final StringProperty nazwa; //name 
    private final ObjectProperty<ProductType> typ; //type 
    private final BooleanProperty dostepnosc; 

    public Product() 
    { 
     this(null, 0, ProductType.ALKOHOL, true); 
    } 


    public Product(String nazwa, int ilosc, ProductType typ, boolean dostepnosc) { 
     this.nazwa = new SimpleStringProperty(nazwa); 
     this.ilosc = new SimpleIntegerProperty(ilosc); 
     this.typ = new SimpleObjectProperty<>(typ); 
     this.dostepnosc = new SimpleBooleanProperty(dostepnosc); 
    } 
. 
. 
. 
@XmlJavaTypeAdapter(EnumAdapter.class) 
    public ProductType getTyp() { 
     return typ.get(); 
    } 

在我的应用程序枚举打开XML后总是设置为从默认的构造函数的值(这是酒精,如果我改变它,枚举设置为任何它)。我也知道EnumAdapter的编组工作正常,我可以将其更改为任何我想要的。请帮忙。

回答

0

我解决了它,我错过了正确的设置功能:

​​