我正在设置一个Import XML文件模块,该模块将从文件中读取数据,并将其解析为表。用户可以通过ElementID搜索其数据,并更改除ElementID之外的所有内容。然后,用户可以将其保存回xml文件中。
我尝试过类似的事情:
从XML读取数据:
private boolean openFile() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open File");
fileChooser.setInitialDirectory(new File("."));
fileChooser.getExtensionFilters().addAll(new ExtensionFilter("XML", "*.xml"));
File selectedFile = fileChooser.showOpenDialog(null);
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(selectedFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Item");
data.retainAll();
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
ItemProperties itemProp = new ItemProperties();
itemProp.setItemId(eElement.getAttribute("IDNumber"));
itemProp.setExperience(eElement.getAttribute("Experience"));
itemProp.setInterPower(eElement.getAttribute("InterPower"));
data.add(itemProp);
}
}
} catch (IOException | ParserConfigurationException | SAXException e) {
}
searchTextField.setDisable(false);
buttonSearch.setDisable(false);
labelFileName.setText("Successfully loaded " + selectedFile.getName());
return true;
}
打开文件事件由一个按钮处理。我将所有属性保存到名为data
private final ObservableList<ItemProperties> data = FXCollections.observableArrayList();
设置tableView
private final TableView<ItemProperties> table = new TableView<>();
table.setEditable(
true);
TableColumn<ItemProperties, String> itemID = new TableColumn<>("Item ID");
itemID.setCellValueFactory(new PropertyValueFactory<>("itemID"));
itemID.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<ItemProperties, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<ItemProperties, String> t) {
((ItemProperties) t.getTableView().getItems().get(t.getTablePosition().getRow()))
.setItemId(t.getNewValue() + "");
}
});
TableColumn<ItemProperties, String> Experience = new TableColumn<>("Experience");
Experience.setCellValueFactory(new PropertyValueFactory<>("Experience"));
Experience.setCellFactory(TextFieldTableCell.<ItemProperties>forTableColumn());
Experience.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<ItemProperties, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<ItemProperties, String> t) {
((ItemProperties) t.getTableView().getItems().get(t.getTablePosition().getRow()))
.setExperience(t.getNewValue() + "");
}
});
TableColumn<ItemProperties, String> interPower = new TableColumn<>("Inter Power");
interPower.setCellValueFactory(new PropertyValueFactory<>("interPower"));
interPower.setCellFactory(TextFieldTableCell.<ItemProperties>forTableColumn());
interPower.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<ItemProperties, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<ItemProperties, String> t) {
((ItemProperties) t.getTableView().getItems().get(t.getTablePosition().getRow()))
.setInterPower(t.getNewValue() + "");
}
});
table.setItems(data);
table.getColumns().addAll(itemID, Experience, interPower);
此表具有可编辑的单元格。我用data
我也创建了一个searchButton,它将通过名为searchData
的新arrayList调用表的searchFuntion和setItems。
private final ObservableList<ItemProperties> searchData = FXCollections.observableArrayList();
public boolean searchElementByID(String searchID) {
buttonBack.setDisable(false);
for (int i = 0; i < data.size(); i++) {
ItemProperties item = this.data.get(i);
if (item.getItemID().equals(searchID)) {
searchData.retainAll();
ItemProperties itemProp = new ItemProperties();
itemProp.setItemId(item.getItemID());
itemProp.setExperience(item.getExperience());
itemProp.setInterPower(item.getInterPower());
searchData.add(itemProp);
searchQuery.setText(searchID + " found!");
return true;
}
}
searchQuery.setText(searchID + " not found!");
return false;
}
搜索按钮
buttonSearch.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
searchElementByID(searchTextField.getText());
table.setItems(searchData);
}
});
我只用一张表来显示所有事件,导入和搜索。我还创建了一个名为“上一步”的按钮,用于将表中的项目从data
设置到searchData
。
buttonBack.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
searchData.retainAll();
table.setItems(data);
}
});
这是一个问题,我尝试在搜索ID后尝试编辑ID的某些属性,并且我知道当我按“后退”按钮时,所有内容都会丢失,并且初始XML数据将返回,而我所做的任何编辑都不会数据已保存。
通过编辑onEditCommit或对backButton进行某些处理,是否可以解决此问题?
很抱歉,很长的帖子。