Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

where to save an XML file to FILESYSTEM and how to open it

  1. Where to store an XML file to Filesystem.applicationDataDirectory

  2. After the xml is stored, how to open this file, just like XHR request to get XML from internet

— asked June 14th 2010 by kai zhang
  • filesystem
  • open
0 Comments

3 Answers

  • var f=Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'data.xml');
    
    f.write(this.responseData); // the response you get
    
    
    
    var file=Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "data.xml"),
    
    if(file.exists()){
    
    Ti.API.info('in file ' + file.nativePath);
    
    var fcontent = file.read();
    
    Ti.API.info('contents = ' + contents.text);
    
    }
    
    — answered June 14th 2010 by Guillaume LAFOUTRY
    permalink
    0 Comments
  • I tired the following code trying to read data.xml. It does not work. Where should I put data.xml in the system.

    var f=Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'data.xml');

    f.write(this.responseData); // the response you get

    var file=Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "data.xml"),

    if(file.exists()){

    Ti.API.info('in file ' + file.nativePath);

    var fcontent = file.read();

    Ti.API.info('contents = ' + contents.text);

    }

    — answered June 14th 2010 by Kyle chang
    permalink
    0 Comments
  • I created a function to get an image from internet only if it does not exists on the device. You could use it in your case with a small change I think.
    log function is just an alias to Ti.API.info
    Take a look and let me know

    var get_remote_file =  function(filename, url, fn_end, fn_progress ) {
        var file_obj = {file:filename, url:url, path: null};
    
        var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
        if ( file.exists() ) {
            file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator;
            fn_end(file_obj);
        }
        else {
    
            if ( Titanium.Network.online ) {
                var c = Titanium.Network.createHTTPClient();
    
                c.setTimeout(10000);
                c.onload = function()
                {
    
                    if (c.status == 200 ) {
                        log('finished downloading '+ filename +' from '+url);
    
                        var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
                        f.write(this.responseData);
                        file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator;
                    }
    
                    else {
                        file_obj.error = 'file not found'; // to set some errors codes
                    }
                    fn_end(file_obj);
    
                };
                c.ondatastream = function(e)
                {
                    log('progress '+ filename +':'+e.progress);
                    if ( fn_progress ) fn_progress(e.progress);
                };
                c.error = function(e)
                {
                    log('error '+e.error);
                    file_obj.error = e.error;
                    fn_end(file_obj);
                };
                c.open('GET',url);
                c.send();            
            }
            else {
                file_obj.error = 'no internet';
                fn_end(file_obj);
            }
    
    
        }
    };
    
    — answered June 14th 2010 by Dan Tamas
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.