我正在尝试使用jdom更新xml文件中的元素,但不知何故,整个操作文件后没有得到更新。下面是我的代码:+
public int editServer(String[] server) {
int response = 0;
int validate = 0;
try {
File inputFile = new File(fileName);
saxBuilder = new SAXBuilder();
document = saxBuilder.build(inputFile);
classElement = document.getRootElement();
List<Element> voiceServers = classElement.getChildren();
for (int temp = 0; temp < voiceServers.size(); temp++) {
Element vServer = voiceServers.get(temp);
serverName = vServer.getChild("ServerName").getText();
type = vServer.getChild("Type").getText();
String metaData[] = { "ServerName", "IP", "Port", "UserName", "Password", "DataBase", "Type", "Status" };
if (serverName.equalsIgnoreCase(server[0].toString()) && type.equalsIgnoreCase(server[6].toString())) {
// 2 : deleted
validate = validateServer(server);
if (server.length == metaData.length && validate != 1) {
for (int i = 1; i < metaData.length; i++) {
System.out.println("Previous Value : " + vServer.getChild(metaData[i]).getText());
vServer.getChild(metaData[i]).setText(server[i]);
System.out.println("New Value : " + vServer.getChild(metaData[i]).getText());
}
System.out.println("DOne Save...");
break;
}
// vServer.getChild("ServerName").setText(text)
} else {
response = 500;
}
}
FileWriter writer = new FileWriter(fileName);
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat());
outputter.output(document, writer);
response = 2;
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return response;
}
以下是我在孩子的值之前和之后打印的控制台响应。