This is my first attempt messing with Flash and reading an xml file. I’ve got the feed loaded, i even have the feed output text into a textbox.
The last bit is i want to get to the total number of nodes that are returned from the xml feed. Also, the XML feed is a wordpress category specific feed.
The structure is like channel -> item -> title, for example. So i think i want to count the number of “item” elements from the feed?
I have tried a bunch of things, but i can’t see to get this one.
Here is what i have so far:
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("{my rss feed}"));
/* This loads the XML */
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
parseData(xmlData);
}
// Here is where i need to get the total number of nodes from my xml file;
// The reason is i want to give my random range function a "maximum" value.
// I'm pulling a random post from the feed
function randomRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
var randomNum = randomRange(0, 3);
function parseData(mTip:XML):void {
flashTip.htmlText = mTip.channel.item[randomNum].description.text();
}
Here is the final script with all edits. I’ve tested it in Flash CS5 using AS3. It is working too. Maybe someone else will find this useful.
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("{my wordpress rss feed url}"));
/* This loads the XML */
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
parseData(xmlData);
}
/* This gets the data for today's tip */
function parseData(mTip:XML):void {
var itemXMLList:XMLList = XMLList(mTip..item);
var count:int = itemXMLList.length();
var finalcount:int = count - 1;
//trace(finalcount);
function randomRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
var randomNum = randomRange(0, finalcount);
//trace(randomNum);
flashTip.htmlText = mTip.channel.item[randomNum].description.text();
}
Try this
Note that double dot means here item node may be at any level (depth) we retrive from XML and it always return XMLList.