Editing XML files using DOM in Android

by rajesh » Thu, 12 Mar 2009 14:56:05 GMT


Sponsored Links
 Hi all ,
I am developing an application where i need to update a value in an
XML file according to the value entered by the user  and send this
updated file to some server.I am using DOM to do this ..but the
problem is that the function setNodeValue does not seem to
work ....When i print the value after changing the content it gives
the same value as before ...

Code:

                         NodeList list = null;
                        DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();


                        //xmlFile = fileName;
                        try
                        {
                                DocumentBuilder db = dbf.newDocumentBuilder();
                                InputStream istream = null;
           istream=(this.mCtx).getResources().openRawResource
(R.raw.currencyconverter);
                            xmlDoc2 = db.parse(istream);

                        Element docEle = xmlDoc2.getDocumentElement();
                        list = docEle.getElementsByTagName("Variable");
                         for (int j = 0; j < list.getLength(); j++)
                    {
                        Node child = list.item(j);
                        if (child.getNodeType() == Node.ELEMENT_NODE) {
                       Element element=(Element)child;
                       setTextValue(element,"contentvalue","1000");
        System.out.println("This is the text value"+getTextValue
(element,"contentvalue"));


                        }
                    }

                        }

                        catch (Exception e)
                        {
                                e.printStackTrace();

                        }
This is the setTextValue method :

 private static void setTextValue(Element ele,String tagName,String
Value)
         {


        try {
                 NodeList nl = ele.getElementsByTagName(tagName);
                 if (nl != null && nl.getLength() > 0)
                 {

                          Element el = (Element) nl.item(0);

                         if (el.getFirstChild() != null)
                         {
                                 el.getFirstChild().setNodeValue(Value);
                              // This is the statement which does not
seem to work

                         }

                 }
        }
            catch (Exception e)
            {

             e.printStackTrace();

            }

         }



Please help.... i am not able to understand what is happening .
THANKS.


--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. image upload

how it is possible to upload image from android handset to the server?
Is the image serialized, send as bytestream and deserialized on the
server? how it is different from javame version of uploading images to
the web service?

-- 

2. shape button

The Button shape is VEry Atractive mode

-- 

3. Drawing a View on top of a Layout

4. Android Freeware Paint

5. Detect bluetooth headset status changes

6. code to call Android Music player Application

7. how to get the subview in the GridView?