1

i would like modify text of xml tag value i have used an xml as follows

  <note>
       <to>Tove</to> 
       <from>Jani</from> 
       <heading>Reminder</heading> 
       <body>Don't forget me this weekend!</body> 
 </note>

here i would like to change the from name Jani as prasad.how can i chage that by using java code

i have written a java code as follows

 try{
     DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
     DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
     Document doc = docBuilder.parse(new File("/mnt/sdcard/one.xml"));

    //Get the staff element by tag name directly
     Node nodes = doc.getElementsByTagName("note").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("from".equals(nodes.getNodeName())){
             node.setNodeValue("prasad");

             TransformerFactory factory = TransformerFactory.newInstance();
             Transformer transformer = factory.newTransformer();

             DOMSource source = new DOMSource(doc);
             StreamResult result = new StreamResult(new File("/mnt/sdcard/one.xml"));
             transformer.transform(source, result);


         }
     }

}
    catch (Exception e) {
        e.printStackTrace();
    }
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
prasad.gai
  • 2,977
  • 10
  • 58
  • 93
  • Whats the problem with your code? Also, if you update names and other tags more often, a [SQLiteDatabase](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html) would be better suited for this job. – Lukas Knuth Jul 07 '11 at 10:45
  • @prasad.gai Hi hi, sorry for disturb u.. just, i have similar problem like you i would like to ask you and advise. My problem is, after try the above code, the node's value of xml file in SD card doesn't change. – April Smith Jan 24 '12 at 04:15

2 Answers2

2

There is problem in your code: "Jani" is a text node with parent element node "from". So you should change value of the text node.

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

         //get the salary element, and update the value
         if("from".equals(node.getNodeName())){
             Text text = (Text) ((Element) node).getChildNodes().item(0);
             text.setNodeValue("prasad");
         }
     }

     TransformerFactory factory = TransformerFactory.newInstance();
     Transformer transformer = factory.newTransformer();

     DOMSource source = new DOMSource(doc);
     StreamResult result = new StreamResult(new File("/mnt/sdcard/one.xml"));
     transformer.transform(source, result);
...

And I think RegExp would be much easier for this task

"your xml as string".replaceAll("<from>.*?</from>", "<from>prasad</from>");
Dmitry
  • 56
  • 3
0

There are several different examples available.

Writing XML on Android This seems to have the best samples.

Other hits: How to rewrite or modify XML in android 2.1 version

Load and modify xml file in Android

Community
  • 1
  • 1
David Olsson
  • 8,085
  • 3
  • 30
  • 38