How do you update an XML file after you change a node?



I'm using Node.setTextContent() to edit a node but it's not changing anything in the file. If I print the text content after it will appear as changed but it will not persist after the program is closed.



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 d=0; d<number of child nodes of month node; d++)
for (int d=0; d<calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().getLength(); d++) {
//label node
node = calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).getChildNodes().item(0);
}
}
}
}
}

System.out.println(node.getTextContent());
node.setTextContent(tf_label.getText());
System.out.println(node.getTextContent());

OutputFormat outFormat = new OutputFormat(xmlDoc);
try (FileOutputStream outStream = new FileOutputStream("src/virtualagenda/Calendar.xml")) {
XMLSerializer serializer = new XMLSerializer(outStream, outFormat);
serializer.serialize(xmlDoc);

outStream.close();
}catch(IOException e) {e.printStackTrace(System.out);}

No comments:

Post a Comment