2017-03-27 67 views
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。哪裏不對?有人能幫助我嗎?

+1

而什麼意思'不起作用'? – Jens

+0

始終unmarhall導致異常,並且不會從XML加載到ObservableList。 – wienio

+0

然後添加stacktrace到你的問題 – Jens

回答

0

有2個問題:

  1. XML文件是無效的。 standalone應該有yes而不是true值。
  2. 那麼,不認爲JAXB將使用參數創建的構造Product屬性。

你需要做一些改變:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<products> 
    <products> 
      <amount>124</amount> 
      <ifConttainsPreservatives>false</ifConttainsPreservatives> 
      <name>Apple</name> 
      <type>FRUITS</type> 
    </products> 
</products> 
public class Product { 
    ... 

    public Product() { 
     // invoke constructor that does assign the properties 
     this(0, false, null, null); 
    } 
} 

請注意,這是一個好主意,使房地產領域最終使編譯器抱怨,如果這樣的字段未初始化/重新分配:

private final StringProperty name; 
private final IntegerProperty amount; 
private ProductType type; 
private final BooleanProperty ifConttainsPreservatives;