Click here to Skip to main content
15,888,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an xml file with a root node called Assignments and if l add a an child element called Assignment it successfully adds with all properties date,course etc....the problem arises if l try adding another assignment instead of adding it overrides the previously inserted data

What I have tried:

try
{
File assigns = new File("xmlfiles/Assignments.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(assigns);

Node rootNode = doc.getDocumentElement();
Element assign = doc.createElement("Assignment");
rootNode.appendChild(assign);

Element modulename = doc.createElement("ModuleName");
modulename.appendChild(doc.createTextNode(txtModule.getText()));
assign.appendChild(modulename);

LocalDate ld = datePicker.getValue();
Element date = doc.createElement("Date");
date.appendChild(doc.createTextNode(ld.toString()));
assign.appendChild(date);

Element somenotes = doc.createElement("Notes");
somenotes.appendChild(doc.createTextNode(txtNotes.getText()));
assign.appendChild(somenotes);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(assigns);
transformer.transform(source, result);

}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900