Java JAXB解组枚举
我的枚举类型ProductType已正确保存为XML,但它不希望在打开文件时解组 我做了一件事:Java JAXB解组枚举,java,xml,enums,jaxb,Java,Xml,Enums,Jaxb,我的枚举类型ProductType已正确保存为XML,但它不希望在打开文件时解组 我做了一件事: public class EnumAdapter extends XmlAdapter<String, ProductType> { @Override public ProductType unmarshal(String value) throws Exception { try { return ProductType.val
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();
}
}
公共类EnumAdapter扩展了XmlAdapter
{
@凌驾
public ProductType解组器(字符串值)引发异常{
试一试{
返回ProductType.valueOf(值);
}
捕获(例外e){
抛出新的jaxbeexception(e);
}
}
@凌驾
公共字符串封送处理(ProductType值){
返回值.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();
}
公共类产品{
私有最终整数属性ilosc;//数量
私有最终StringProperty nazwa;//名称
私有最终ObjectProperty类型;//类型
私有最终布尔财产dostepnosc;
公共产品()
{
这是(null,0,ProductType.ALKOHOL,true);
}
公共产品(字符串nazwa、int ilosc、ProductType typ、布尔dostepnosc){
this.nazwa=新的SimpleStringProperty(nazwa);
this.ilosc=新的SimpleIntegerProperty(ilosc);
this.typ=新的SimpleObject属性(typ);
this.dostepnosc=新的SimpleBoleAnProperty(dostepnosc);
}
.
.
.
@XmlJavaTypeAdapter(EnumAdapter.class)
公共产品类型getTyp(){
返回类型get();
}
在我的应用程序中打开XML后,enum始终设置为默认构造函数中的值(即酒精,如果我更改它,enum将设置为任何值)。我还知道EnumAdapter中的封送可以正常工作,我可以将其更改为我想要的任何值。请帮助。我解决了它,我缺少正确的设置函数:
public void setTyp(ProductType type){
this.typ.setValue(type);