where to save an XML file to FILESYSTEM and how to open it
Where to store an XML file to Filesystem.applicationDataDirectory
After the xml is stored, how to open this file, just like XHR request to get XML from internet
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); }
-
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);
}
-
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 knowvar 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); } } };