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. Problem with HTTP connection

Hi I am trying to establish the connection between mobile and PC.I am
running server program on PC, when I run the below code server not
displaying any response message.When I send the request from mobile
browser server displaying response message. I am working on
SDK_1.1.Please suggest me on program
code........................................

public class HTTPPostExample extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
try{
httpPostImage();
}
catch(Exception e)
{}
}
}
public void httpPostImage() throws Exception {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://157.182.195.189:6666/");

//File file = new File("/data/data/org.emrah/icon.png");
// InputStreamEntity reqEntity = new InputStreamEntity(new
FileInputStream(file), -1);


reqEntity.setContentType("application/x-www-form-urlencoded");

Header[] headers = new BasicHeader[2];
headers[0] = new BasicHeader("Accept","application/xml");
headers[1] = new BasicHeader("Content-type", "application/x-www-form-
urlencoded");
httppost.addHeader(headers[0]);
httppost.addHeader(headers[1]);

//httppost.setEntity(reqEntity);

System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

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

2. findViewById() returns NULL

I was getting a null exception in my onCreate() until I found that I
had to move that method call _after_ the
setContentView(R.layout.main), just in case anyone else has this same
problem. Don't understand the reason why, but just in case anyone gets
this (and then googles for it), this is a possible cause.

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

3. Multiple Instances of App Running When Home + Restart

4. Web Browser stop Selection

5. How to receive NEW_OUTGOING_CALL?

6. 2D accleration, request OpenGL

7. UberColorPicker Demo