Titanium Community Questions & Answer Archive

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

It still doesnot work. Looks like system can not find the xml file. Where should i put the file?

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 contents = file.read();

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

}

— asked June 14th 2010 by Kyle chang
  • xml
0 Comments

9 Answers

  • Can you post the entire code ?

    — answered June 14th 2010 by Guillaume LAFOUTRY
    permalink
    0 Comments
  • Thanks, here is the entire code, the xml on cloud is ok to get and read, but not on the local system.

    var data = [];

    var xhr = Ti.Network.createHTTPClient();

    if (Titanium.Network.online == true)

    {
    xhr.open("GET","http://www.cdn.com/data.xml");
    }

    else {

    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 contents = file.read();

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

    }

    }

    — answered June 15th 2010 by Kyle chang
    permalink
    0 Comments
  • Kyle, I don't see Xhr onload to write responseData to the file, that's why it's not there. Also, you are rewriting the file when network is not there from responseData.

    // so here, if network is ok, you store data.xml in the system
    if (Titanium.Network.online == true)
    { 
    xhr.open("GET","http://www.cdn.com/data.xml"); 
    xhr.onload = function(){
      var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'data.xml');
    
    f.write(this.responseData);
    Ti.API.info('new file written');
    }
    } 
    else{
    //otherwise you read file from previously stored data
    var f=Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "data.xml"),
    
    if(f.exists()){
    
    Ti.API.info('in file ' + f.nativePath);
    
    var contents = f.read();
    
    Ti.API.info('contents = ' + contents.text);
    
    }
    
    else{
    Ti.API.info('boo boo, file not there');
    }
    }
    
    — answered June 15th 2010 by Daniel Lim
    permalink
    0 Comments
  • Daniel,
    Thanks a lot for ur help. I put your code to my file. Looks like it work, but it generates a new error, which is from another part of the file. Here is the entire code:
    if (Titanium.Network.online == true)
    {
    xhr.open("GET","http://www.cdn.com/data.xml");
    xhr.onload = function(){
    var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'data.xml');

    f.write(this.responseData);
    Ti.API.info('new file written');
    }
    }
    else{
    //otherwise you read file from previously stored data
    var f=Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "data.xml"),

    if(f.exists()){

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

    var contents = f.read();

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

    }

    else{
    Ti.API.info('boo boo, file not there');
    }
    }

    xhr.onload = function()
    {
    try
    {

        var results = this.responseXML.documentElement;
        //var pubdate = results.evaluate("//feed/updated/text()");
        var pubdate = results.getElementsByTagName("updated").item(0).text;       
        //var title = results.getElementsByTagName("title");
        //var elist = results.getElementsByTagName("entry");
        var items = results.getElementsByTagName("entry");
        var x = 0;
        var oldsection = "none";
        if(!items){
            alertBox.show();
            }
        else {}
        ----------
    

    the error message is " Result of expression "this.responseXML' [null] is not an object."

    i donot have this error message until i put ur code in.
    thanks a lot

    — answered June 15th 2010 by Kyle chang
    permalink
    0 Comments
  • try this to get a dom object:

    var xml= Titanium.XML.parseString(responseXml);

    — answered June 15th 2010 by Guillaume LAFOUTRY
    permalink
    1 Comment
    • Tried this, still get error message like this:
      "Can't find variable: responseXML"

      not sure what's going on
      thanks

      — commented June 15th 2010 by Kyle chang
  • Still get the error message: can not find variable: responseXML

    not sure what caused this.

    thanks

    — answered June 15th 2010 by Kyle chang
    permalink
    0 Comments
  • You need to put the 2nd xhr onload on the first XHR onload block.

    xhr.onload = function(){
      var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'data.xml');
    f.write(this.responseData);
     var results = this.responseXML.documentElement;
        //var pubdate = results.evaluate("//feed/updated/text()");
        var pubdate = results.getElementsByTagName("updated").item(0).text;       
        //var title = results.getElementsByTagName("title");
    .............
    ..............
    }
    

    If you are trying to access remote xml on load, store it for both online and offline use. I would create function to parse the xml elemments so you don't have to repeat it twice.

    
    var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'data.xml');
    
    if (Titanium.Network.online == true) { 
     var xhr = Ti.Network.createHTTPClient();
           xhr.open("GET","http://www.cdn.com/data.xml"); 
           xhr.onload = function(){       
              f.write(this.responseData);
              getXMLdata(f); 
     } 
    else{ 
    if(f.exists()){
              getXMLdata(f); 
    }
    else{
        Titanium.UI.createAlertDialog({ 
                        title:'Network Error',
                        message: 'Your device is not online.'
                    }).show();
    }
    
    }
    
    function getXMLdata(file){
    if(file.exists()){
       var content = file.read();
       var results = Ti.XML.parseString(content.text);
       //access dom elements from results
     }
     else {
     alert('oops, file not there.')
     }
    
    — answered June 15th 2010 by Daniel Lim
    permalink
    0 Comments
  • Daniel, Thanks for your help. I tried to put the 2nd xhr onload on the first XHR onload block. Looks like it doesnot work. Here is the entire code of this file:

    var win = Titanium.UI.currentWindow;
    win.setBarColor('#165a99');
    win.showNavBar();

    // create table view data object

    var data = [];

    var xhr = Ti.Network.createHTTPClient();

    if (Titanium.Network.online == true)
    {
    xhr.open("GET","http://www.abcd.com/output3.xml");
    xhr.onload = function(){
    var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'output3.xml');

    f.write(this.responseData);
    Ti.API.info('new file written');
    };
    }
    else{
    //otherwise you read file from previously stored data

    var f=Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "output3.xml");

    if(f.exists()){

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

    var contents = f.read();

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

    }

    else{
    Ti.API.info('boo boo, file not there');
    }
    }

    //activity indicator
    var actInd = Titanium.UI.createActivityIndicator({
    bottom:200,
    height:50,
    width:10,
    style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
    });

    actInd.style = Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN;
    actInd.font = {fontFamily:'Helvetica Neue', fontSize:15,fontWeight:'bold'};
    actInd.color = 'white';
    actInd.message = 'Loading Table of Contents...';
    actInd.width = 210;
    actInd.show();
    

    win.add(actInd);

    xhr.onload = function()
    {
    try
    {
    var results = this.responseXML.documentElement;
    //var pubdate = results.evaluate("//feed/updated/text()");
    var pubdate = results.getElementsByTagName("updated").item(0).text;
    //var title = results.getElementsByTagName("title");
    //var elist = results.getElementsByTagName("entry");
    var items = results.getElementsByTagName("entry");
    var x = 0;
    var oldsection = "none";
    if(!items){
    alertBox.show();
    }
    else {}

        //build top-most heading for dates
        var headingRow = Ti.UI.createTableViewRow({
                height:30,
                className:"headingrow"
            });        
    
            var label = Ti.UI.createLabel({
                    text:'Issue date: ' + pubdate,
                    top:5,
                    height:'auto',
                    font:{fontSize:14},
                    left:5,
                    //color:'#fff',
                    right:5
                });
            headingRow.add(label);            
            data[x++] = headingRow;
    
        for (var c=0;c<items.length;c++)
        {
            var entry = items.item(c);
            var section = entry.getElementsByTagName("section").item(0).text;
            var title = entry.getElementsByTagName("title").item(0).text;
            var authors = entry.getElementsByTagName("authors").item(0).text;
            var category = entry.getElementsByTagName("category").item(0).text;
            var thumbnail = entry.getElementsByTagName("media:thumbnail").item(0).text;
    
                                    //var link = linklist.item(0).text;
    
            var row = Ti.UI.createTableViewRow({height:'auto',className:"myrow" }); 
    
    
            if(section != oldsection){
                row.header=section;
            }
    
            var individualRow = Ti.UI.createView({ //this sets the title/summary etc into its own window, so the image can be seperate.
                height:'auto',
                layout:'vertical',
                top:5,
                bottom:5,
                //minRowHeight:90,
                right:10
                });
    
    
            var rowcategory = Ti.UI.createLabel({
                text:category,
                left:72,
                height:15,
                font:{fontSize:12},
                right:5
            });
    
            var rowtitle = Ti.UI.createLabel({
                text:title,
                left:72,
                height:50,
                font:{fontSize:12,fontWeight:'bold'},
                right:5
            });
    
    
    
            var img = Ti.UI.createImageView({
                url:thumbnail,
                top:5,
                //bottom:5, //this causes problems with centering and stuff
                height:60,
                width:60,
                left:5,
                borderColor:'#000'
            });
    
            //row.add(rowtitle);
            //row.add(img);
            //for just the title, uncomment above, and delete the below row setup
            individualRow.add(rowcategory);
            individualRow.add(rowtitle);
            row.add(individualRow);
            row.add(img);           
    
    
    
            row.link = entry.getElementsByTagName("link").item(0).text;
            row.article = entry.getElementsByTagName("content").item(0).text;
            row.articledate = entry.getElementsByTagName("published").item(0).text;
            row.articletitle = entry.getElementsByTagName("title").item(0).text;
            row.vol = results.getElementsByTagName("vol").item(0).text;
            row.fpage = entry.getElementsByTagName("fpage").item(0).text;
            row.articleauthors = authors;
            row.image = thumbnail;
    
            data[x++] = row;
            oldsection = section;
    
    
        }
    
        var tableview = Titanium.UI.createTableView({
            data:data,
            className:"Thetableview"    
            });
    
    
        //once everything is loaded, then hide the activity notifier       
        actInd.hide();
    
    
        Titanium.UI.currentWindow.add(tableview);
        tableview.addEventListener('click',function(e)      
        //what happens when an article gets clicked
        {
    
            if (Ti.Platform.name == 'android') {
                options.navBarHidden = true;
            }
            var articleWindow = Titanium.UI.createWindow({
    
                    backgroundColor:'#165a99',
                    //title:'Latest News',
                    barColor:'#165a99'
    
            });
    
    
    
            var webView = Titanium.UI.createWebView({
    
                top:0,
    
                html:"<html><body><table><tr><td valign=\"top\"><img src=\""+e.row.image+"\" /></td><td valign=\"top\"><span style=\"color:#7777777;font-size:10px\">"+e.row.articledate+" - Volume "+e.row.vol+", Page "+e.row.fpage+"</span><h3 style=\"margin-top:2px\">"+e.row.articletitle+"</h3></td></tr><tr><td colspan=\"2\"> <span style=\"color:#666666\">"+e.row.articleauthors+"</span></td></tr></table>"+e.row.article+"<p></p><hr />View the full text <a href=\""+e.row.link +"\">here</a></body></html>"
            });
    
    
    
            var emailButton = Titanium.UI.createButton({
                title:'E-mail this',
                backgroundColor:'#165a99'
            });
    
            articleWindow.rightNavButton = emailButton;
            articleWindow.add(webView);
    
            Titanium.UI.currentTab.open(articleWindow,{animate:false});
    
    
            closeButton.addEventListener('click', function()
            {
    
                Titanium.UI.currentTab.close(articleWindow,{animate:false});
            });
    
            emailButton.addEventListener('click', function()
            {
                var emailDialog = Titanium.UI.createEmailDialog();
                emailDialog.setSubject('Article on Science Online');
                emailDialog.setMessageBody('From Science Online:  \n\n'+e.row.articletitle+'\n\n'+e.row.link + '\n\n Get Science Magazine on your iPhone for free by visiting http://itunes.com/apps/sciencemag');
                emailDialog.setBarColor('#d70000');
                //emailDialog.open(descriptionWindow,{animated:true});
                emailDialog.open();
            });
    
    
    
        });
    }
    catch(E)
    {
        alert(E);
    }
    

    };
    xhr.send();

    — answered June 17th 2010 by Kyle chang
    permalink
    0 Comments
  • Here's the question for you, what are you trying to accomplish?

    — answered June 17th 2010 by Daniel Lim
    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.