无法解组XML文件

时间:2018-12-09 07:44:12

标签: xml javafx jaxb

这里是JavaFX的新功能。我有一个应用程序,可以将网站数据与XML文件之间进行保存和加载。

Website模型用于ObservableList,根据我的理解,您无法将@XmlElement附加到ObservableList,所以我为其添加了一个包装器(基本上遵循本教程:{{ 3}})。

不过,当我尝试取消封送处理时,此行代码中引发了异常:

        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;
    }

}

0 个答案:

没有答案