title: How to append an existing XML file using DOM without overwriting the existing data? in java
body: <p>i have a very confused about the how the xml data can be append to already existed data give me your suggestions my code is like this </p>
  
  <pre><code> Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();//db.newDocument();//create document
  
          Element root = doc.createElement("Employees");//cretae Elements
          doc.appendChild(root);
  
          Comment cmt = doc.createComment("Employee Details");//Add comment to xml
          root.appendChild(cmt);
  
          Element employee = doc.createElement("employee");//create Element
          //employee.appendChild(doc.)
          root.appendChild(employee);
  
          Attr genderAttr = doc.createAttribute("Gender");
          System.out.print("Enter your gender :");
          String gend = br.readLine();
          genderAttr.setValue(gend);
          employee.setAttributeNode(genderAttr);
  
          System.out.print("Enter first name:");
          String child = br.readLine();
          Element FName = doc.createElement("firstName");
          FName.appendChild(doc.createTextNode(child));//set xml text
          employee.appendChild(FName);
  
          System.out.print("Enter last name:");
          String child1 = br.readLine();
          Element LName = doc.createElement("lastName");            
          LName.appendChild(doc.createTextNode(child1));
          employee.appendChild(LName);
  
          //root.appendChild(employee);
          //doc.appendChild(root);
  
          //to write on file/screen
          TransformerFactory tf = TransformerFactory.newInstance();
          Transformer tr = tf.newTransformer();
          tr.setOutputProperty(OutputKeys.INDENT, "yes");
          DOMSource source = new DOMSource(doc);//source             
          //File shopOrder = new File("src"+File.separator+"xmlparsing"+File.separator+"xmlParse1.xml");//get the file
          StreamResult res = new StreamResult(new File("src"+File.separator+"xmlparsing"+File.separator+"xmlParse1.xml"));//Destination
          tr.transform(source, res);//to write on file
  </code></pre>
  
  <p>and i can do easily parse and update also but i can't understand how i can append previous data please help me</p>