[英]Java JAXB unmarshaling Enum
我的Enum类型ProductType已正确保存为XML,但在打开文件时不希望解组。
我做了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之后,枚举始终设置为默认构造函数的值(ALCOHOL,如果我更改它,则枚举设置为它的任何值)。 我也知道从EnumAdapter编组工作正常,我可以将其更改为所需的任何内容。 请帮忙。
我解决了,我缺少正确的设置功能:
public void setTyp(ProductType type){
this.typ.setValue(type);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.