Robbiegod
Robbiegod

Reputation: 1014

Flash AS3 XML - How can i count the number of items from my xml feed?

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();

 }

Upvotes: 0

Views: 2025

Answers (2)

Raja Jaganathan
Raja Jaganathan

Reputation: 36187

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.

var itemXMLList:XMLList = XMLList(mTip..item);
var count:int = itemXMLList.length();

Upvotes: 2

Josh
Josh

Reputation: 8159

If I am understanding your question correctly, you get it with the length() method.

trace( mTip.property1[0].property2[0].property3.length() ); //will return however many property3s there are in mTip.property1[0].property2[0]

Take notice that in the XML class, length is not a property as it is with String, Array, Vector, and any other number of classes. It is a method. I make this mistake quite often and it is definitely wrong.

If you want all the nodes in the document and not just a specific one, I don't know of a quick way (not that there isn't one). I would probably just loop through each node and count it manually.

Upvotes: 0

Related Questions