Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save & update the values in xml file?

I am reading a xml file from the SD card. Here I want to change the values of the XML file and I want to save the file to the sd card..

My code is like below.... Please guide me how to save XML file to the sd card after updating the values..

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");
             }
         }
like image 291
MUKTHA Avatar asked Jul 07 '11 06:07

MUKTHA


2 Answers

Something like this:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
StreamResult result = new StreamResult(file);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
like image 198
padis Avatar answered Oct 21 '22 10:10

padis


how to modify xml node value?

like image 29
prasad.gai Avatar answered Oct 21 '22 09:10

prasad.gai