How to download XML file?

You must Login before you can answer or comment on any questions.

I have a PHP script on my server that parses through a web page and creates an XML file for the data, so that I can more easily consume it through my app. Now I know I can parse XML through XHR and whatnot, however, I don't want to be constantly redownloading the file, since the data is not going to change very often.

So, basically my question is, is there a way to download an XML file and store it on the device or in the app somehow, so that I can then load and parse that? Rather than having to constantly go to the server and download it every time I start the app?

Thanks for any help!

1 Answer

This is what i use to read stored xml when no network connectivity, else access new data. Help yaself.

if (Titanium.Network.networkType == Titanium.Network.NETWORK_NONE){
  var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'store.xml');
    if(f.exists()){
             var content = f.read();
             var doc = Ti.XML.parseString(content.text);
             ///blah blah with node value and elements
     }
else{
      Ti.UI.createAlertDialog({title:'Network Error', message:'APP requires network connectivity to load data on first use.'}).show();
 }
} //end network check
else{ //network present
var xhr = Ti.Network.createHTTPClient();
xhr.open("POST","some_remote_access_xml.xml");
xhr.onerror = function(e)
        {
            Ti.UI.createAlertDialog({title:'Network Error', message:'Unable to retrieve data.'}).show();
            Ti.API.info('IN ERROR ' + e.error);
        };
xhr.setTimeout(30000);
xhr.onload = function(){ 
    try{
        var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'store.xml');
        var doc = this.responseXML.documentElement;
        if (doc.getElementsByTagName("success").item(0).text==1){
               f.write(this.responseData);      
        } //end if succesul
    else{Ti.UI.createAlertDialog({title:'Service Error', message:'Service is not available at the moment, please try it again.'}).show();}}
    catch(E){Ti.UI.createAlertDialog({title:'Application Error', message:'Unable to obtain data, please try it later.'}).show();}   
}; //end try
xhr.send();
}

Your Answer

Think you can help? Login to answer this question!