Tag: 文件写

在向其添加节点后,如何立即更新XML文档?

所以我正在创建一个日历程序,当你向它添加一个新条目时我需要它来更新。 现在,我需要点击xml文件让它更新,然后其他一切正常。 宣言: private DocumentBuilderFactory documentFactory; private DocumentBuilder documentBuilder; private Document xmlDoc; private Node rootNode; private static Node dataNode; 构造函数中的赋值: try { documentFactory = DocumentBuilderFactory.newInstance(); documentBuilder = documentFactory.newDocumentBuilder(); xmlDoc = documentBuilder.parse(Main.class.getResourceAsStream(“Calendar.xml”)); rootNode = xmlDoc.getDocumentElement(); dataNode = rootNode.getChildNodes().item(0); } catch(ParserConfigurationException | SAXException | IOException e) {e.printStackTrace(System.out);} 按下按钮后,节点被创建并添加到dataNode ,然后文件更新如下: try { OutputFormat outFormat = new OutputFormat(xmlDoc); try […]

更改节点后如何更新XML文件?

我正在使用Node.setTextContent()来编辑节点,但它没有改变文件中的任何内容。 如果我打印文本内容后,它将显示为已更改但在程序关闭后不会保留。 for (int y=0; y<calendarDataNode.getChildNodes().getLength(); y++) { //if (year node name == "y" + current year) if (calendarDataNode.getChildNodes().item(y).getNodeName().equals("y" + Main.year)) { //for (int m=0; m<number of child nodes of year node; m++) for (int m=0; m<calendarDataNode.getChildNodes().item(y).getChildNodes().getLength(); m++) { //if (month node name == "m" + current month) if (calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getNodeName().equals("m" + (Main.month_index-1))) { //for (int […]