Skip to main content
Participating Frequently
April 3, 2014
Question

How to parse RSS Feed - Different URL for each button

  • April 3, 2014
  • 1 reply
  • 535 views

I'm making an app with AS3 and I want to be able to load a different RSS Feed when the user clicks on each button. This is the code I have so far, has anyone any idea how I can modify my code to do this?

here's my code so far:

//IMPORT CLASSES

import fl.controls.List;

import flash.net.URLLoader;

import flash.events.Event;

import flash.net.URLRequest;

cGOSSIP.addEventListener(MouseEvent.CLICK, doClick);

function doClick (e:MouseEvent):void

{

cGOSSIP.gotoAndStop ("GOSSIP"); 

}

wNEWS.addEventListener(MouseEvent.CLICK, doClick2);

function doClick2 (e: MouseEvent):void

{

          wNEWS.gotoAndStop ("WORLD NEWS");

}

eNEWS.addEventListener(MouseEvent.CLICK, doClick3);

function doClick3 (e: MouseEvent):void

{

          eNEWS.gotoAndStop ("ENT NEWS");

}

fbNEWS.addEventListener(MouseEvent.CLICK, doClick4);

function doClick4 (e: MouseEvent):void

{

          fbNEWS.gotoAndStop ("FB NEWS");

}

//NEWSLIST FORMATTING

var newsListTf:TextFormat = new TextFormat();

newsListTf.font = "Verdana";

newsListTf.color = 0xFFFFFF;

newsListTf.size = 18;

newsListTf.align = "left";

newsList.setRendererStyle("textFormat", newsListTf);

newsList.rowHeight = 100;

newsList.allowMultipleSelection = false;

//newsList.wordWrap = true;

//NEWSDISPLAY TEXT AREA FORMATTING

var rssStoryTf:TextFormat = new TextFormat();

rssStoryTf.font = "Verdana";

rssStoryTf.color = 0x6699CC;

rssStoryTf.size = 22;

rssStoryTf.align = "left";

rssStory.setStyle("textFormat", rssStoryTf);

//READMORE BUTTON FORMATTING

var buttonTf:TextFormat = new TextFormat();

buttonTf.font = "Verdana";

buttonTf.color = 0xFFFFFF;

buttonTf.size = 22;

btn_ReadMore.setStyle("textFormat", buttonTf);

//RSS FEED

var rssLoader:URLLoader = new URLLoader();

var rssURL:URLRequest = new URLRequest

("http://rssfeeds.tv.adobe.com/adobe-higher-education-solutions.xml");

rssLoader.addEventListener(Event.COMPLETE, rssLoaded);

newsList.addEventListener(Event.CHANGE, selectRssStory);

btn_ReadMore.addEventListener(MouseEvent.CLICK, goToURL);

rssLoader.load(rssURL);

var newsXML:XML = new XML();

var currentStoryURL:URLRequest = new URLRequest();

newsXML.ignoreWhitespace = true;

function rssLoaded(evt:Event):void

{

newsXML = new XML(rssLoader.data);

//trace(newsXML);

for(var item:String in newsXML.channel.item) {

//newsList.addItem({label: newsXML.channel.item[item].pubDate.substr(0, 16)

//+": " + newsXML.channel.item[item].title } );

newsList.addItem({label: newsXML.channel.item[item].title.substr(0, 40)

+" ..." } );

}

newsList.selectedIndex = 0;

//FIRST LOAD

currentStoryURL = new URLRequest(newsXML.channel.item[0].link);

rssStory.htmlText = newsXML.channel.item[0].description;

}

function selectRssStory(evt:Event):void

{

rssStory.htmlText = newsXML.channel.item[evt.target.selectedIndex ].description;

currentStoryURL = new URLRequest(newsXML.channel.item[evt.target.selectedIndex

].link);

}

function goToURL(event:MouseEvent):void

{

navigateToURL(currentStoryURL);

}

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
April 3, 2014

Does the code currently work at all to load an RSS feed?  If so, youy should only need to change the URLREquest value for the loading function

Participating Frequently
April 3, 2014

I copied the wrong code, it works and loads the feed but I have a number of buttons in the main menu and I want to be able to load a different rss feed for each button?

Here's the code.

btn_Home.addEventListener(MouseEvent.CLICK, clickHome);

function clickHome(e:MouseEvent):void

{

          gotoAndStop ("START");

}

//NEWSLIST FORMATTING

var newsListTf:TextFormat = new TextFormat();

newsListTf.font = "Verdana";

newsListTf.color = 339999;

newsListTf.size = 18;

newsListTf.align = "left";

newsList.setRendererStyle("textFormat", newsListTf);

newsList.rowHeight = 100;

newsList.allowMultipleSelection = false;

//newsList.wordWrap = true;

//NEWSDISPLAY TEXT AREA FORMATTING

var rssStoryTf:TextFormat = new TextFormat();

rssStoryTf.font = "Verdana";

rssStoryTf.color = 0x6699CC;

rssStoryTf.size = 22;

rssStoryTf.align = "left";

rssStory.setStyle("textFormat", rssStoryTf);

//READMORE BUTTON FORMATTING

var buttonTf:TextFormat = new TextFormat();

buttonTf.font = "Verdana";

buttonTf.color = 0xFFFFFF;

buttonTf.size = 22

btn_ReadMore.setStyle("textFormat", buttonTf);

//RSS FEED

var rssLoader:URLLoader = new URLLoader();

var rssURL:URLRequest = new URLRequest

("http://www.tmz.com/rss.xml");

rssLoader.addEventListener(Event.COMPLETE, rssLoaded);

newsList.addEventListener(Event.CHANGE, selectRssStory);

btn_ReadMore.addEventListener(MouseEvent.CLICK, goToURL);

rssLoader.load(rssURL);

var newsXML:XML = new XML();

var currentStoryURL:URLRequest = new URLRequest();

newsXML.ignoreWhitespace = true;

function rssLoaded(evt:Event):void

{

newsXML = new XML(rssLoader.data);

//trace(newsXML);

for(var item:String in newsXML.channel.item) {

//newsList.addItem({label: newsXML.channel.item[item].pubDate.substr(0, 16)

//+": " + newsXML.channel.item[item].title } );

newsList.addItem({label: newsXML.channel.item[item].title.substr(0, 40)

+" ..." } );

}

newsList.selectedIndex = 0;

//FIRST LOAD

currentStoryURL = new URLRequest(newsXML.channel.item[0].link);

rssStory.htmlText = newsXML.channel.item[0].description;

}

function selectRssStory(evt:Event):void

{

rssStory.htmlText = newsXML.channel.item[evt.target.selectedIndex ].description;

currentStoryURL = new URLRequest(newsXML.channel.item[evt.target.selectedIndex

].link);

}

function goToURL(event:MouseEvent):void

{

navigateToURL(currentStoryURL);

}

Ned Murphy
Legend
April 4, 2014

create a function that processes the rssLoader.load command instead of having it outside any function like you show.  Then you can have your buttons call that function.  If your buttons arew movieclips you can assign the url as a property to each one and read the url in the loading function that way (like you do for the currentStoryURLin the selectRssStory function) 

If that is the function that processes the buttons you are talking about, you can process the loading right in that function or have it call the separate loading function.