Comment mettre à jour des fichiers xml en java

J'ai un fichier xml appel data.xml comme le code ci-dessous. Le projet peut exécuter à partir du côté client, pas de problème et il peut lire le fichier xml. Le problème que j'ai maintenant c'est que je veux écrire une fonction qui permet de mettre à jour la date de début et date de fin. Je n'ai aucune idée de comment obtenir le départ. Aide sera appréciée.

  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data>
       <username>admin</username>
       <password>12345</password>
       <interval>1</interval>
       <timeout>90</timeout>
       <startdate>01/01/2013</startdate>
       <enddate>06/01/2013</enddate>
       <ttime>1110</ttime>
    </data>

mon main.java

    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 < 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 < 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);
}
stackoverflow.com/questions/6124239/... et stackoverflow.com/questions/7646607/...
Au moins montrer votre code qu'avez-vous essayé

OriginalL'auteur Big Ticket | 2015-07-29