title: a stray <?xml version="1.0" encoding="UTF-8" standalone="no"?> appearing while appending the xml file
body: <p>I am trying to write a function that will store the name and date of Birth into an .xml file. The code should look if the .xml file exists already or not. when the file doesn't exist, a new .xml is created and the data is entered. But when the function is called for the second time, a stray  appears while appending. Kindly let me know how to fix this. I am new to java and xml parsing and I have just started with it today. The function that deals with xml creation and appending is xmlCreator()
  I am pasting the code below</p>
  
  <pre><code>   import java.awt.*;
     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import javax.swing.*;
     import java.io.FileWriter;
     import java.nio.file.*;
     import javax.xml.parsers.DocumentBuilder;
     import javax.xml.parsers.DocumentBuilderFactory;
     import javax.xml.transform.OutputKeys;
     import javax.xml.transform.Transformer;
     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;
     public class Exercise1 extends JPanel implements ActionListener{
  
  
    String name;
    String dateOfBirth;
  
   //Labels to identify the fields
   JLabel nameLabel;
   JLabel dateOfBirthLabel;
  
   //Strings for the labels
   static String nameString = "Name";
   static String dateOfBirthString = "Date Of Birth (dd/mm/yyyy)";
  
   JTextField nameField;
   JTextField dateOfBirthField;
  
   JButton clearButton;
   JButton saveAndProceedButton;
   JButton bdayInNext5Days;
  
   int serialNumber = 0;
   FileWriter xmlFile;
  
   public Exercise1()
   {
   super (new BorderLayout());
  
   nameLabel = new JLabel(nameString);
   dateOfBirthLabel = new JLabel(dateOfBirthString);
  
   nameField = new JTextField ();
   nameField.setColumns(20);
   nameLabel.setLabelFor(nameField);
  
   dateOfBirthField = new JTextField ();
   dateOfBirthField.setColumns(20);
   dateOfBirthLabel.setLabelFor(dateOfBirthField);
  
   //Lay out the labels in a panel.
   JPanel labelPane = new JPanel(new GridLayout(0,1));
   labelPane.add(nameLabel);
   labelPane.add(dateOfBirthLabel);
  
   //Layout the text fields in a panel.
   JPanel fieldPane = new JPanel(new GridLayout(0,1));
   fieldPane.add(nameField);
   fieldPane.add(dateOfBirthField);
  
   clearButton = new JButton ("Clear");
   clearButton.addActionListener (this);
  
   saveAndProceedButton = new JButton ("Save and Proceed");
   saveAndProceedButton.addActionListener (this);
   serialNumber++;
  
   bdayInNext5Days = new JButton("Bday in next 5 days");
   bdayInNext5Days.addActionListener (this);
   //Put the panels in this panel, labels on left,
   //text fields on right.
   add(labelPane, BorderLayout.LINE_START);
   add(fieldPane, BorderLayout.CENTER);
   add(clearButton, BorderLayout.LINE_END);
   add(saveAndProceedButton, BorderLayout.SOUTH);
   add(bdayInNext5Days, BorderLayout.NORTH);
   }
  
   /**
    * Create the GUI and show it.  For thread safety,
    * this method should be invoked from the
    * event dispatch thread.
    */
   private static void createAndShowGUI() {
       //Create and set up the window.
       JFrame frame = new JFrame("Exercise 1");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
       //Add contents to the window.
       frame.add(new Exercise1());
  
       //Display the window.
       frame.pack();
       frame.setVisible(true);
   }
  
   private void xmlCreator(String name, String dateOfBirth) throws Exception{
  
       Path path = Paths.get("Bday.xml");
       DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
       DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
       Document document = null;
       Element element = null;
       Transformer transformer = null;
       TransformerFactory transformerFactory = null;
  
       if(Files.exists(path)) { 
           xmlFile = new FileWriter("Bday.xml",true);
           document = documentBuilder.parse("Bday.xml");
           element = document.getDocumentElement();
       }
       else{
           xmlFile = new FileWriter("Bday.xml",true);
           document = documentBuilder.newDocument();
           element = document.createElement("Birthday");
           document.appendChild(element);
       }
       Element Id = document.createElement("Id");
       Id.appendChild(document.createTextNode(Integer.toString(serialNumber)));
       element.appendChild(Id);
  
       Element xmlName = document.createElement("Name");
       xmlName.appendChild(document.createTextNode(name));
       Id.appendChild(xmlName);
  
       Element xmldateOfBirth = document.createElement("Date_Of_Birth");
       xmldateOfBirth.appendChild(document.createTextNode(dateOfBirth));
       Id.appendChild(xmldateOfBirth);
  
       DOMSource source = new DOMSource(document);
       transformerFactory = TransformerFactory.newInstance();
       transformer = transformerFactory.newTransformer();
       transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  
       StreamResult streamResult = new StreamResult(xmlFile);
  
       transformer.transform(source, streamResult);
  
  }
   public void actionPerformed(ActionEvent e) {
          Object source = e.getSource();
  
          if (source == clearButton){
              nameField.setText("");
              dateOfBirthField.setText("");
          }
          else if(source == saveAndProceedButton){
              name = nameField.getText();
              dateOfBirth = dateOfBirthField.getText();
              try {
                  xmlCreator(name,dateOfBirth);
              } catch (Exception e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
              nameField.setText("");
              dateOfBirthField.setText("");
              serialNumber++;
          }
          else{
  
  
          }
  
  
      }
  
  
  public static void main (String[] args)
   {
         SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                   //Turn off metal's use of bold fonts
                  UIManager.put("swing.boldMetal", Boolean.FALSE);
                   createAndShowGUI();
               }
           });
   }}
  </code></pre>
  
  <p>The output stored in the .xml file is given below</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;Birthday&gt;
  &lt;Id&gt;1&lt;Name&gt;asdasd&lt;/Name&gt;
  &lt;Date_Of_Birth&gt;123&lt;/Date_Of_Birth&gt;
  &lt;/Id&gt;
  &lt;/Birthday&gt;
  &lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;Birthday&gt;
  &lt;Id&gt;1&lt;Name&gt;asdasd&lt;/Name&gt;
  &lt;Date_Of_Birth&gt;123&lt;/Date_Of_Birth&gt;
  &lt;/Id&gt;
  &lt;Id&gt;2&lt;Name&gt;adss&lt;/Name&gt;
  &lt;Date_Of_Birth&gt;12&lt;/Date_Of_Birth&gt;
  &lt;/Id&gt;
  </code></pre>
  
  <p></p>