节点不会保存到xml中的文件中

时间:2015-02-16 16:28:04

标签: java xml dom

我一直在尝试将节点保存到java文件中" killsdeaths.xml"但经过几个小时的调试后,它似乎能够正确检索节点中的值,但它并没有保存它。有人可以帮忙吗?

File f = new File("KillsDeaths.xml");
StatsRetrieve sr = new StatsRetrieve();
@SuppressWarnings("deprecation")
public int[] giveKills(Player player, int kills) throws Exception{
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder;
    docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(f);
    OfflinePlayer offlineplayer = Bukkit.getOfflinePlayer(player.getName());
    offlineplayer.getUniqueId().toString();
    Node root = sr.getPlayerByUUID((Player)offlineplayer);
    Node killsnode = root.getFirstChild();
    int kills2 = Integer.parseInt(killsnode.getTextContent());
    int killsadded = kills2 + kills;
    String backtostring = Integer.toString(killsadded);
    killsnode.setTextContent(backtostring);
    killsnode.setNodeValue(backtostring);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(f);
    transformer.transform(source, result);
    Bukkit.getLogger().info("Done.");
    int[] array = {kills2, killsadded};
    return array;
}

1 个答案:

答案 0 :(得分:0)

除非我失明,否则您忘记将节点附加到文档对象。