当他们点击退出按钮时,尝试这样做,他们的名字和金额存储在XML文件中。我还要阅读那里的当前XML文件
在向XML文件添加新节点时遇到问题。
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse("scores.xml");
Element rootElement = document.createElement("scores");
document.appendChild(rootElement);
Element player = document.createElement("player");
Element playerName = document.createElement("name");
Element moneyAmount = document.createElement("money");
playerName.appendChild(document.createTextNode(name));
playerName.appendChild(document.createTextNode(String.valueOf(money)));
player.appendChild(playerName);
DOMSource source = new DOMSource(document);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
StreamResult result = new StreamResult("scores.xml");
transformer.transform(source, result);
System.exit(0);
}
catch (ParserConfigurationException ex) {
ex.printStackTrace();
} catch (SAXException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
当前XML:
<scores>
<player>
<name>Chief Sosa</name>
<money>1000</money>
</player>
</scores>
我想添加其他玩家 期望的:
<scores>
<player>
<name>Chief Sosa</name>
<money>1000</money>
</player>
<player>
<name>Kobe</name>
<money>11000000</money>
</player>
</scores>
答案 0 :(得分:0)
为您提供2种可能的解决方案:
1。通过setAttribute函数定义您的节点ID。之后,xml文件将如下所示
<scores id ="scoreId"> </scores>
当您想要添加其他玩家时。使用以下命令查找具有ID的节点:
Node n = document.getElementById("scoreId");
创建一个播放器 最后是这个节点中的apendChild。
2。另一种解决方案是使用document.getElementsByTagName("scores");
在此结果中循环并附加您的节点。
希望这个帮助
答案 1 :(得分:0)
这假设一个预先存在的分数文件......
首先获取文档的“根”节点...
Element rootElement = document.getDocumentElement();
然后,而不是尝试从变量name
和money
追加“文本”......
// This is creating a mess
Element player = document.createElement("player");
Element playerName = document.createElement("name");
Element moneyAmount = document.createElement("money");
playerName.appendChild(document.createTextNode(name));
playerName.appendChild(document.createTextNode(String.valueOf(money)));
player.appendChild(playerName);
您想要设置适当的节点文本内容
Element player = document.createElement("player");
Element playerName = document.createElement("name");
playerName.setTextContent(name);
Element moneyAmount = document.createElement("money");
moneyAmount.setTextContent(NumberFormat.getNumberInstance().format(money));
接下来,您要将名称和moneny节点附加到player
元素...
player.appendChild(playerName);
player.appendChild(moneyAmount);
然后将player
节点附加到“root”
rootElement.appendChild(player);
<强>更新强>
您可以使用以下内容......
Document document = null;
File scores = new File("scores.xml");
if (scores.exists()) {
document = documentBuilder.parse("scores.xml");
} else {
document = documentBuilder.newDocument();
document.appendChild(document.createElement("scores"));
}
要加载或创建新文档(如果它不存在),请按照前面的示例使用附加代码......
更新了示例...
使用...
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = null;
File scores = new File("scores.xml");
if (scores.exists()) {
document = documentBuilder.parse("scores.xml");
} else {
document = documentBuilder.newDocument();
document.appendChild(document.createElement("scores"));
}
Element rootElement = document.getDocumentElement();
String name = "Kobe";
double money = 11000000;
Element player = document.createElement("player");
Element playerName = document.createElement("name");
playerName.setTextContent(name);
Element moneyAmount = document.createElement("money");
moneyAmount.setTextContent(NumberFormat.getNumberInstance().format(money));
player.appendChild(playerName);
player.appendChild(moneyAmount);
rootElement.appendChild(player);
DOMSource source = new DOMSource(document);
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty(OutputKeys.METHOD, "xml");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
StreamResult result = new StreamResult(scores);
tf.transform(source, result);
} catch (ParserConfigurationException | SAXException | IOException | TransformerException ex) {
ex.printStackTrace();
}
从...开始
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<scores>
<player>
<name>Chief Sosa</name>
<money>1,000</money>
</player>
</scores>
我能够生成
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<scores>
<player>
<name>Chief Sosa</name>
<money>1,000</money>
</player>
<player>
<name>Kobe</name>
<money>11,000,000</money>
</player>
</scores>