如何使JAR文件更改XML文件?

时间:2017-11-08 23:07:51

标签: java xml jar

我试图通过jar文件重写一个xml文件。现在,jar文件可以读取xml文件,但它无法重写它。这是代码:



public static void addMoney(int money) {
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(SaveData.class.getResourceAsStream("SaveData.xml"));
            Element udata = doc.getDocumentElement();
            udata.normalize();
            Node mnode = udata.getElementsByTagName("money").item(0);
            int mony = Integer.parseInt(mnode.getTextContent())+money;
            mnode.setTextContent(""+mony);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File("SaveData.xml"));
            transformer.transform(source, result);
        } catch(Exception e) { e.printStackTrace(); }
    }




我认为错误在于我如何编写StreamResult结果。

0 个答案:

没有答案