-1

I have the below code that writes to an xml file, the problem that I am having is that it creates a new file every time i write to it and overwrites the other data saved in the file. I am looking for a solution that would append to the existing file instead. How do I modify this code to append to the file each time instead of overwrite? Also, I am using the netbeans IDE to run this program.

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element; 
import org.xml.sax.SAXException;
public class WriteXMLFile {

    public static void main() throws ParserConfigurationException,SAXException,Exception
    {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();//
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();//

        Document doc = docBuilder.newDocument();// this is difrent

        Element rootElement = doc.createElement("Contacts");//
        doc.appendChild(rootElement); // this is difrent
        // staff elements

        Element Contact1 = doc.createElement("Contact1");
        rootElement.appendChild(Contact1);
        // set attribute to staff element

        Contact1.setAttribute("id","1");

        // firstname elements

        Element firstname = doc.createElement("Name");
        firstname.appendChild(doc.createTextNode(EmailFrame.name.getText()));
        Contact1.appendChild(firstname); 

        //Email Element
        Element email = doc.createElement("Email");
        email.appendChild(doc.createTextNode(EmailFrame.email.getText()));
        Contact1.appendChild(email); 

        // phone element
        Element phone= doc.createElement("Phone");
        phone.appendChild(doc.createTextNode(EmailFrame.phone.getText()));
        Contact1.appendChild(phone);
        //id element
        Element id = doc.createElement("ID");
        id.appendChild(doc.createTextNode(EmailFrame.id.getText()));
        Contact1.appendChild(id); 

        try{
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);


            StreamResult result = new StreamResult(new File("C:/Users/steve/Desktop/xmlemail/Email.xml"));

            transformer.transform(source, result);
            System.out.println("File saved!");
        }
        catch (TransformerException tfe) {
            tfe.printStackTrace();
        }
    }
}
Steven
  • 180
  • 3
  • 3
  • 12

1 Answers1

2

What you need to do is read the contents of the xml file into an object first before writing, then append your new content to your object then write your object to the xml file

Look at this resource for reading an xml file it should help

https://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

Ayo K
  • 1,719
  • 2
  • 22
  • 34