Subject: read write xml file

Original Post hirva patel's Avatar

hirva patel

08 Feb, 2010 08:39 AM via web

hi
I am on Mac and on Titanium 0.8.2. i am building for Android platform.

i have a database.xml file in my resources. i read that file using the following code:

var readscoreContents;

        var xmlfilename = Titanium.Filesystem.getResourcesDirectory() + "database.xml";

        Titanium.API.info(xmlfilename); 

        var readscoreFile = Titanium.Filesystem.getFile(Titanium.Filesystem.getResourcesDirectory(), "database.xml");  

       if (readscoreFile.exists())
       {  
          readscoreContents = readscoreFile.read();  
       }

    var xmlscoreDoc;
    var xmlscoreString =readscoreContents.toString();

   if (document.implementation.createDocument) {
        var sparser = new DOMParser();
        xmlscoreDoc = sparser.parseFromString(xmlscoreString, "text/xml");

    } else if (window.ActiveXObject) {
        xmlscoreDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlscoreDoc.async="false";
        xmlscoreDoc.loadXML(xmlscoreString);
    }

then i create a new node to append in the xml file.

var root2 = xmlscoreDoc.getElementsByTagName("scores")[0];

    var newel=xmlscoreDoc.createElement("score");
    root2.appendChild(newel);

Now i need to save this file to reflect the changes in database.xml file.
Please guide me with the code to save the xml file.

This is my database.xml file structure :

<?xml version="1.0" encoding="UTF-8"?>

<scores>
</scores>

I want to add a "score" tag to this file inside "scores" tag.

Thanks

  1. Support Staff 2 Posted by Kevin Whinnery on 08 Feb, 2010 04:52 PM

    Kevin Whinnery's Avatar
  2. 3 Posted by hirva patel on 09 Feb, 2010 02:37 AM

    hirva patel's Avatar

    hi

    i modified the code to

    http://pastie.org/815670

    i get this error...

    [ERROR] checking: Resource file may not be written.

    any help

  3. Support Staff 4 Posted by Don Thorp on 09 Feb, 2010 02:54 AM

    Don Thorp's Avatar

    You can't write to the resources directory. You can write to the
    ApplicationDataDirectory

    hth

  4. 5 Posted by hirva patel on 09 Feb, 2010 02:57 AM

    hirva patel's Avatar

    ok thanks for the reply.

Comments are currently closed for this discussion. You can start a new one.

Recent Discussions

13 Mar, 2010 01:38 PM
13 Mar, 2010 12:39 PM
13 Mar, 2010 11:06 AM
13 Mar, 2010 10:58 AM
13 Mar, 2010 09:02 AM