Sunday, July 13, 2014

titanium: how to get data from internet. eg.: RSS, how to process xml

 var url = "http://www.channelnewsasia.com/rss/latest_cna_frontpage_rss.xml"; //"http://www.appcelerator.com";

 var client = Ti.Network.createHTTPClient({
     // function called when the response data is available
     onload : function(e) {
         Ti.API.info("Received text: \n" + this.responseText);
         Ti.API.info("Received xml: \n" + this.responseXML);
         //alert('success');
         process(this.responseXML);
     },
     // function called when an error occurs, including a timeout
     onerror : function(e) {
         Ti.API.debug(e.error);
         //alert('error');
     },
     timeout : 5000  // in milliseconds
 });
 // Prepare the connection.
 client.open("GET", url);
 // Send the request.
 client.send();

view.add(textarea);
self.add(view);


function process(xml){
    var items = xml.getElementsByTagName("item");
    for(var i = 0; i        Ti.API.info("items " + i + " - " + items.item(i).getElementsByTagName("title").item(0).textContent);
        Ti.API.info("content " + i + " - " + items.item(i).getElementsByTagName("description").item(0).textContent);
    }
}


No comments: