这里是JavaFX的新手 . 我有一个应用程序,可以保存和加载XML文件的网站数据 .

Website 模型在ObservableList中使用,从我的下面,你不能将 @XmlElement 附加到ObservableList,所以我为它添加了一个包装器(基本上遵循本教程:https://code.makery.ch/library/javafx-tutorial/part5/) .

当我尝试解组它时,在这行代码中引发异常:

WebsiteListWrapper wrapper = (WebsiteListWrapper) um.unmarshal(file);

这是我的其余代码:

MainApp.java:

public void loadWebsiteDataFromFile(File file) {

        try {
            JAXBContext context = JAXBContext.newInstance(WebsiteListWrapper.class);

            Unmarshaller um = context.createUnmarshaller();

            WebsiteListWrapper wrapper = (WebsiteListWrapper) um.unmarshal(file);
            System.out.println(um.unmarshal(file));
            websiteData.clear();
            websiteData.addAll(wrapper.getWebsites());

            setWebsiteFilePath(file);
        } catch (Exception e) {
            Alert alert = new Alert(AlertType.ERROR);
            alert.setTitle("Error");
            alert.setHeaderText("Could not load data");
            alert.setContentText("Could not load data from file:\n" + file.getPath());

            alert.showAndWait();
        }
    }

Website.java (型号):

public class Website {

    private final StringProperty website;
    private final BooleanProperty accountExists;
    private final BooleanProperty keep;
    private final BooleanProperty delete;

    public Website() {
        this(null, true);
    }

    public Website(String website, boolean accountExists) {
        this.website = new SimpleStringProperty(website);
        this.accountExists = new SimpleBooleanProperty(accountExists);
        this.keep = new SimpleBooleanProperty(false);
        this.delete = new SimpleBooleanProperty(false);
    }


    public StringProperty websiteProperty() {
        return website;
    }

    public BooleanProperty hasAccountProperty() {
        return accountExists;
    }

    public BooleanProperty keepProperty() {
        return keep;
    }

    public BooleanProperty deleteProperty() {
        return delete;
    }

    public String getWebsite() {
        return website.get();
    }

    public boolean getAccountExists() {
        return accountExists.get();
    }

    public boolean getKeep() {
        return keep.get();
    }

    public boolean getDelete() {
        return delete.get();
    }


    public void setWebsite(String website) {
        this.website.set(website);
    }

    public void setAccountExists(boolean bool) {
        accountExists.set(bool);
    }

    public void setKeep(boolean bool) {
        keep.set(bool);
    }

    public void setDelete(boolean bool) {
        delete.set(bool);
    }

}

WebsiteListWrapper

@XmlRootElement(name = "websites")
public class WebsiteListWrapper {

    private List<Website> websites;

    @XmlElement(name = "website")
    public List<Website> getWebsites() {
        return websites;
    }

    public void setPersons(List<Website> websites) {
        this.websites = websites;
    }

}