title: How to update xml files in java
body: <p>I have a xml file call data.xml like the code below. The project can run from client side no problem and it can read the xml file. The problem I have now is I I want to write a function that can update the startdate and enddate. I have no idea how to get start. Help will be appreciated.</p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;data&gt;
         &lt;username&gt;admin&lt;/username&gt;
         &lt;password&gt;12345&lt;/password&gt;
         &lt;interval&gt;1&lt;/interval&gt;
         &lt;timeout&gt;90&lt;/timeout&gt;
         &lt;startdate&gt;01/01/2013&lt;/startdate&gt;
         &lt;enddate&gt;06/01/2013&lt;/enddate&gt;
         &lt;ttime&gt;1110&lt;/ttime&gt;
      &lt;/data&gt;
  </code></pre>
  
  <p>my main.java</p>
  
  <pre><code>    public class main
      {
       public static void main(String[] args) 
        {
            Calendar cal2 =null;
  
      try {   
  
                //read the xml      
                File data = new File("data.xml");  
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();         
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();           
                Document doc = dBuilder.parse(data);         
                doc.getDocumentElement().normalize();
  
       for (int i = 0; i &lt; nodes.getLength(); i++) {     
                Node node = nodes.item(i);           
                  if (node.getNodeType() == Node.ELEMENT_NODE) {     
                      Element element = (Element) node;   
                      username = getValue("username", element);
                      startdate = getValue("startdate", element);
                      enddate = getValue("enddate", element);
                    }
         }
  
  
    date = startdate; 
    Date date_int = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(date); 
    cal2 = Calendar.getInstance(); 
   cal2.setTime(date_int); 
  
  
       //loop the child node to update the initial date
                for (int i = 0; i &lt; nodes.getLength(); i++) {    
                    Node node = nodes.item(i);           
                      if (node.getNodeType() == Node.ELEMENT_NODE) {     
                          Element element = (Element) node;
  
                          setValue("startdate", element , date_int.toString());
                    }
                }
  
              //write the content in xml file
                  TransformerFactory transformerFactory = TransformerFactory.newInstance();
                  Transformer transformer = transformerFactory.newTransformer();
                  DOMSource source = new DOMSource(doc);
                  StreamResult result = new StreamResult(new File("data.xml"));
                  transformer.transform(source, result);
  
          } catch (Exception ex) {    
            log.error(ex.getMessage());       
            ex.printStackTrace();       
          }
      }
  
  
        private static void setValue(String tag, Element element , String input) {  
              NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();   
              Node node = (Node) nodes.item(0); 
              node.setTextContent(input);
  
      }
  </code></pre>