2011-07-07 10 views
6

Sto leggendo un file xml dalla scheda SD. Qui voglio cambiare i valori del file XML e voglio salvare il file nella scheda SD ..Come salvare e aggiornare i valori nel file xml?

Il mio codice è come sotto .... Per favore guidami come salvare il file XML sulla scheda SD dopo l'aggiornamento dei valori ..

public void modifyNodeval(){ 
     try{ 
     DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 
     Document doc = docBuilder.parse(new File("/sdcard/sss.xml")); 

     //Get the staff element by tag name directly 
     Node nodes = doc.getElementsByTagName("Employee").item(0); 
     //loop the staff child node 
     NodeList list = nodes.getChildNodes(); 

     for (int i =0; i<list.getLength();i++){ 
      Node node = list.item(i); 

      //get the salary element, and update the value 
      if("Emp_Name".equals(node.getNodeName())){ 
       node.setNodeValue("795796"); 
      } 
     } 

risposta

1

Qualcosa di simile a questo:

Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
StreamResult result = new StreamResult(file); 
DOMSource source = new DOMSource(doc); 
transformer.transform(source, result); 
0

questo metodo scrive un documento DOM a un file sulla scheda SD Se si desidera verificare questo nell'emulatore, assicurarsi che l'immagine AVD sia impostata con un'immagine della scheda SD (eseguita durante la creazione dell'immagine).

public static void writeXmlFile(Document doc, String filename) { 
    try { 
     // Prepare the DOM document for writing 
     Source source = new DOMSource(doc); 

     File file new File(Environment.getExternalStorageDirectory(),fileName); 

     Result result = new StreamResult(file); 

     // Write the DOM document to the file 
     Transformer xformer = TransformerFactory.newInstance().newTransformer(); 
     xformer.transform(source, result); 
    } catch (TransformerConfigurationException e) { 
     // handle exception 
    } catch (TransformerException e) { 
     // handle exception 
    } 
} 
Problemi correlati