title: Writing text to XML is taking long time --Java
body: <p>I have some 3000 elements in arraylist.Need to write these 3000 elements to xml.
  The execution is taking too much time like 20 minutes in eclipse.
  Is there any efficient way to do this?
  OR any modifications to my code?</p>
  
  <p>The elements in arraylist are supposed to grow in future...</p>
  
  <p>MY code snippet..</p>
  
  <pre><code>---------------------------------------------------
  ---------------------------------------------------
  for(int i=0;i&lt;candidates.size();i++)//Candidates is my arraylist
              {
                  String text=candidates.get(i);
                  //System.out.println(text);
                  text=text+"\n";
                  file= new File("./test.xml");
                  WriteToXML wr= new WriteToXML(file,"fullname",text);
  
              }
  -------------------------------------------------------
  -------------------------------------------------------
  //WritetoXML class constructor
  public WriteToXML(File xml,String tag,String data)
  {
      try {
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          dbf.setValidating(false);
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc = db.parse(new FileInputStream(new File(xml)));
          Element element =  doc.getDocumentElement();
          NodeList node1 = doc.getElementsByTagName(tag);
          Element fn= (Element) node1.item(0);
          Text text = doc.createTextNode(data);
          fn.appendChild(text);
          printtoXML(doc);
      } catch (Exception e) {
              System.out.println(e.getMessage());
              }
  }
  public static final void printtoXML(Document xml) throws Exception {
      Transformer tf = TransformerFactory.newInstance().newTransformer();
      tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
      tf.setOutputProperty(OutputKeys.INDENT, "yes");
      StringWriter sw = new StringWriter();
      StreamResult result = new StreamResult(sw); 
      DOMSource source = new DOMSource(xml);
      tf.transform(source, result);
      String xmlString = sw.toString();
      File file= new File(xml);
      FileWriter fw=new FileWriter(file,false);
      BufferedWriter bw = new BufferedWriter(fw);
      bw.write(xmlString);
      bw.flush();
      bw.close(); 
  }
  </code></pre>