0
我有小問題,解組。讓我們來看看這個方法:JAXB - 反編組不起作用
public void loadProductDataFromFile (File file) {
try {
JAXBContext context = JAXBContext.newInstance(ProductListWrapper.class);
Unmarshaller um = context.createUnmarshaller();
ProductListWrapper wrapper = (ProductListWrapper) um.unmarshal(file); // this always cause exception
products.clear();
products.addAll(wrapper.getProducts());
setProductFilePath(file);
} catch (Exception e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Fail!");
alert.setHeaderText("Can't read data!");
alert.setContentText("Can't read data from:\n" + file.getPath());
alert.showAndWait();
}
}
,這是我ProductListWrapper:
@XmlRootElement(name = "products")
public class ProductListWrapper {
private List<Product> products;
@XmlElement(name = "products")
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products=products;
}
}
我的XML文件:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<products>
<products>
<amount>124</amount>
<ifConttainsPreservatives>false</ifConttainsPreservatives>
<name>Apple</name>
<type>FRUITS</type>
</products>
</products>
而我從我的ObservableList保存方法(即作品我認爲):
public void saveProductsDataToFile(File file) {
try {
JAXBContext context = JAXBContext.newInstance(ProductListWrapper.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ProductListWrapper wrapper = new ProductListWrapper();
wrapper.setProducts(products);
m.marshal(wrapper, file);
setProductFilePath(file);
} catch (Exception e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Fail!");
alert.setHeaderText("Can't save data!");
alert.setContentText("Can't save data to:\n" + file.getPath());
alert.showAndWait();
}
個我Product.class:
public class Product {
private StringProperty name;
private IntegerProperty amount;
private ProductType type;
private BooleanProperty ifConttainsPreservatives;
public void setType(ProductType type) {
this.type = type;
}
public String getName() {
return name.get();
}
public boolean isIfConttainsPreservatives() {
return ifConttainsPreservatives.get();
}
public void setName(String name) {
this.name.set(name);
}
public void setAmount(int amount) {
this.amount.set(amount);
}
public void setIfConttainsPreservatives(boolean ifConttainsPreservatives) {
this.ifConttainsPreservatives.set(ifConttainsPreservatives);
}
public ProductType getType() {
return type;
}
public BooleanProperty ifConttainsPreservativesProperty() {
return ifConttainsPreservatives;
}
public StringProperty nameProperty() {
return name;
}
public int getAmount() {
return amount.get();
}
public IntegerProperty amountProperty() {
return amount;
}
public Product(int amount, boolean ifContainsPreservatives, String name, ProductType type) {
this.name = new SimpleStringProperty(name);
this.amount = new SimpleIntegerProperty(amount);
this.ifConttainsPreservatives = new SimpleBooleanProperty(ifContainsPreservatives);
this.type =type;
}
public Product() {
}
}
類型有IntegerProperty,ProductType(ENUM),StringProperty和BooleanProperty。哪裏不對?有人能幫助我嗎?
而什麼意思'不起作用'? – Jens
始終unmarhall導致異常,並且不會從XML加載到ObservableList。 – wienio
然後添加stacktrace到你的問題 – Jens