user389391
user389391

Reputation: 97

ActionScript RSS Title issue

The below AS# RSS reader code pull the titles from the RSS items but seem to include the XML markup as well. How does one not include the XML markup without using Regex or string replace?

    import flash.text.TextField;
import flash.text.TextFormat;
import flash.net.URLLoader;
import flash.events.IOErrorEvent;


//Read RSS feeds
var RSS_xmlData: XML = new XML();
var xmlLoader: URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("http://www.oshawa.ca/news_rss.asp"));

function LoadXML(e:Event):void {
    dtext.text="Loading...";
    RSS_xmlData = new XML(e.target.data);
    pullFeed(RSS_xmlData);

}

function pullFeed(rss: XML):void {      
    var str: String="";
    str = rss.channel.item.title;
    str = str.replace(/\s*\n/g," | ");
    //str = str.replace(/'/g,"\"");

    //// shows specific entry  
    var items: Array = new Array();
    items = str.split("|");

    var tf: TextField = dtext;
    var i:Number=0;
    var myTimer:Timer = new Timer(4000,1000);
    myTimer.addEventListener(TimerEvent.TIMER, timerListener);
    function timerListener (e:TimerEvent):void{
        tf.text = items[i].toString();
        scaleTextToFitInTextField(tf);
        i = i < items.length - 1 ? i + 1 : 0;
    }
    myTimer.start();
}

function scaleTextToFitInTextField(txt: TextField):void {
    var f: TextFormat = txt.getTextFormat();
    f.size = (txt.width > txt.height) ? txt.width : txt.height;
    txt.setTextFormat(f);

    while (txt.textWidth > txt.width - 4 || txt.textHeight > txt.height - 6) {
        f.size = int(f.size) - 1;
        txt.setTextFormat(f);
    }
}


function onIOError(e:IOErrorEvent):void
{
    trace(e.toString());
    dtext.text="Finding Feed...";
}

Thanks for any help with this.

Upvotes: 0

Views: 54

Answers (1)

hanenbro
hanenbro

Reputation: 604

The reason you're getting the XML markup in there is because the value returned by rss.channel.item.title is of type XMLList, corresponding to all the <title> nodes matched by that selection - rather than just the text content of those nodes. As you've noted, it's rather backwards to convert that to a String and then strip out the extraneous mark-up manually.

I would iterate over all the <item> nodes and add their <title> content to the array as you go along. The new pullFeed method would then look like:

function pullFeed(rss: XML):void {      
    var items:Array = new Array();                 //Declare Array of titles
    var numItems:int = rss.channel.item.length();  //Capture number of <item> nodes

    for (var i:int = 0; i < numItems; i++) {
        items.push(String(rss.channel.item[i].title)); //Add each <item>'s <title> to the Array 
    }

    var tf: TextField = dtext;
    var i:Number=0;
    var myTimer:Timer = new Timer(4000,1000);
    myTimer.addEventListener(TimerEvent.TIMER, timerListener);
    function timerListener (e:TimerEvent):void{
        tf.text = items[i];
        scaleTextToFitInTextField(tf);
        i = i < items.length - 1 ? i + 1 : 0;
    }
    myTimer.start();
}

One other benefit of iterating through the XML nodes this way is that you could easily capture the <description> or <pubDate> content of each <item> on the same pass through the XML, if you planned to make use of that later.

Upvotes: 1

Related Questions