我的RSS Feed问题

时间:2011-04-26 13:45:18

标签: iphone rss appcelerator

即使他们给我加载的物品数量也没有任何东西

这是代码

    var url = 'my url';

// loadRRSFeed(url) // is at the bottom of the js - after all the functions

// useful for getting rid of html links in text elements in a feed
Titanium.include('strip_tags.js');  


///////////////////////////////////////////////////////
var rssWindow = Titanium.UI.createWindow({  
    title:'',
    backgroundColor:'#fff'
});

var data;
var i = 0;
var feedTableView;
var feedTitle = '';


function displayItems(itemList){

    for (var c=0;c < itemList.length;c++){  

        // Ti.API.info('item title :' + itemList.item(c).getElementsByTagName("title").item(0).text);
        // Ti.API.info('item description :' + itemList.item(c).getElementsByTagName("description").item(0).text);
        // Ti.API.info('item enclosure url :' + itemList.item(c).getElementsByTagName("enclosure").item(0).getAttribute("url"));

        var title = null;
        var desc = null;
        //var mp3_url = null;

        // If we want to only add items with mp3 enclosures
        //if(itemList.item©.getElementsByTagName("enclosure")!=null){

            // Item title
            title = itemList.item(c).getElementsByTagName("TITRE").item(0).text;
            // Item description
            desc = itemList.item(c).getElementsByTagName("DESCRIPTION").item(0).text;
            // Clean up any nasty linebreaks in the title and description           
            title = title.replace(/\n/gi, " ");         
            desc = desc.replace(/\n/gi, " ");

            // Podcast mp3 enclosure
            //mp3_url = itemList.item(c).getElementsByTagName("enclosure").item(0).getAttribute("url");

            // Create a table row for this item
            var row = Ti.UI.createTableViewRow({height:'auto',backgroundColor:'#eeeeee',selectedBackgroundColor:'#b40000'}); 

            // Create a label for the title
            var post_title = Ti.UI.createLabel({
                text: title,
                color: '#000',
                textAlign:'left',
                left:60,
                height:'auto',
                width:'auto',
                top:3,
                font:{fontWeight:'bold',fontSize:16}
            });
            row.add(post_title);

            // add the CNN logo on the left
            // naturally this could be an image in the feed itself if it existed
            var item_image = Ti.UI.createImageView({
                image:'My url image',
                left:3,
                top:2,
                width:50,
                height:34
            });
            row.add(item_image);

            // Add some rowData for when it is clicked          
            row.thisTitle = title;
            //row.thisMp3 = mp3_url;
            row.thisDesc = desc;

            // Add the row to the data
            data[i] = row;
            // I use 'i' here instead of 'c', as I'm only adding rows which have mp3 enclosures
            i++;

        } // end if enclosure       
    }

    // create the table
    feedTableView = Titanium.UI.createTableView({
        data:data,
        top:0,
        width:320,
        height:260
    });

    // Add the tableView to the current window 
    rssWindow.add(feedTableView);

    // Create tableView row event listener
    feedTableView.addEventListener('click', function(e){

        // a feed item was clicked
        Ti.API.info('item index clicked :'+e.index);
        Ti.API.info('title  :'+e.rowData.thisTitle);
        Ti.API.info('desc  :'+strip_tags(e.rowData.thisDesc));


        //Ti.API.info('mp3 enclosure  :'+e.rowData.thisMp3);
        // show an alert
        // Ti.UI.createAlertDialog({title:e.rowData.thisTitle, message:e.rowData.thisMp3}).show();

        item_title_label.text = strip_tags(e.rowData.thisTitle);
        item_desc_label.text = strip_tags(e.rowData.thisDesc);
        // etc ...  
        // now do some cool stuff! :)
        // like add an audio player, open a new window, etc..

        //stream.stop();
        //stream.url = e.rowData.thisMp3;
        //stream.start();
    });
//}

function loadRSSFeed(url){

    data = [];
    Ti.API.info('>>>> loading RSS feed '+url);
    xhr = Titanium.Network.createHTTPClient();
    xhr.open('GET',url);
    xhr.onload = function()
    {

        Ti.API.info('>>> got the feed! ... ');

        // Now parse the feed XML 
        var xml = this.responseXML;

        // Find the channel element 
        var channel = xml.documentElement.getElementsByTagName("EPISODE");

        feedTitle = channel.item(0).getElementsByTagName("TITRE").item(0).text;

        Ti.API.info("FEED TITLE " + feedTitle);

        rssWindow.title = feedTitle;

        // Find the RSS feed 'items'
        var itemList = xml.documentElement.getElementsByTagName("EPISODE");
        Ti.API.info('found '+itemList.length+' items in the RSS feed');

        //item_title_label.text = 'DONE';
        //item_desc_label.text = 'click a feed item';

        // Now add the items to a tableView
        displayItems(itemList);

    };

    //item_title_label.text = 'LOADING RSS FEED..';
    //item_desc_label.text = '';
    xhr.send(); 
}



// load the feed
loadRSSFeed(url);

rssWindow.open();

我的xml文件

<DATAS>
<EPISODES>
<EPISODE>
<ID>6712</ID>
<THEME>19</THEME>
<PROGRAMME>90</PROGRAMME>
<DATE_DEBUT>2011/04/25 12:05:00.0</DATE_DEBUT>
<DATE_FIN>2011/04/25 12:09:00.0</DATE_FIN>
<ALIAS></ALIAS>
<TITRE></TITRE>
<TITREPANELPREV></TITREPANELPREV>
<TITREPLAYER></TITREPLAYER>
<TITREPANELNEXT></TITREPANELNEXT>

<DESCRIPTION></DESCRIPTION>
<VIGNETTE>35.jpg</VIGNETTE>
<VIDEO>mp4:35</VIDEO>
<VIDEO_M>mp4:201104</VIDEO_M>
<CHAPITRES/>
</EPISODE><EPISODE>
<ID>6644</ID>
<THEME>16</THEME>
<PROGRAMME>125</PROGRAMME>
<DATE_DEBUT>2011/04/22 18:40:00.0</DATE_DEBUT>
<DATE_FIN>2011/04/22 18:44:00.0</DATE_FIN>
<ALIAS></ALIAS>
<TITRE></TITRE>
<TITREPANELPREV></TITREPANELPREV>
<TITREPLAYER></TITREPLAYER>
<TITREPANELNEXT></TITREPANELNEXT>
<DESCRIPTION></DESCRIPTION>
<VIGNETTE>2011.jpg</VIGNETTE>
<VIDEO>mp4:201104</VIDEO>
<VIDEO_M>mp4:3G.mp4/playlist.m3u8</VIDEO_M>
<CHAPITRES/>
…...etc 

</DATAS>

1 个答案:

答案 0 :(得分:0)

你尝试过吗? :

feedTitle = channel.item(0).getElementsByTagName("TITRE").text;
相关问题