XML Question

I wanted to know if anyone could tell me how to iterate through the XML nodes on a timer. An example of this would be if I had a set of sports scores and I wanted to have them show one game result after the other.

if this is my XML:

<NHL>
  <Team Host="Visiting">
    <Name>Dallas</Name>
    <Scores>
      <Total>1</Total>
    </Scores>
  </Team>
  <Team Host="Visiting">
    <Name>Detroit</Name>
    <Scores>
      <Total>4</Total>
    </Scores>
  </Team>
  <Team Host="Visiting">
     <Name>Pittsburgh</Name>
    <Scores>
      <Total>4</Total>
    </Scores>
  </Team>
  <Team Host="Visiting">
    <Name>Philadelphia</Name>
    <Scores>
      <Total>3</Total>
    </Scores>
  </Team>
</NHL>

So I want the games to come in one at a time like a sports ticker. So it would show

Dallas 1
Detroit 4

then it would wait a certain amount of time and than show:

Pittsburgh 4
Philadelphia 3

I am sure this is possible and if anyone can help it would be greatly appreciated.

Here is some code I was playing with from Kirupa to get the info from the XML I am using

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("MLB.xml"));

function LoadXML(e:Event):void {

    xmlData = new XML(e.target.data);
    ParseSportsXML(xmlData);

}
function ParseSportsXML(SportInput:XML):void {

var GameList:XMLList  = SportInput.Content.Sport.Scoreboard.Game.Team.Name.children();
var GameListAttributesH:XMLList  = SportInput.Content.Sport.Scoreboard.Game.Team.(@Host  ==  "Home").Name;
var GameListAttributesV:XMLList  = SportInput.Content.Sport.Scoreboard.Game.Team.(@Host  ==  "Visiting").Name;
    
    for each (var GameAttributesH:XML in GameListAttributesH) {
        trace("HOME " + GameAttributesH);
        home.text = GameAttributesH;

    }
    for each (var GameAttributesV:XML in GameListAttributesV) {
        trace("VISITOR " + GameAttributesV);
        

    }
    
}

Thanks.

While my AS 3 is kind of flimsy what you would need to do is simply create a timer that would call the next array index that stores the XML data

  1. Load the XML
  2. Parse the XML and store into arrays
  3. Create function that shows index array position
  4. Create timer that call that function and increments position

Hope that helps.

Well I understand step 1. Other than that I am confused. How do I parse the XML into an array? What is the code for creating a function that shows those index array positions? How do I create a timer?

I am very new to AS3 and never had to do this stuff when I was working with AS2 so any help, or links to tutorials that you know of online would be amazing.

Thanks again.

[quote=Digitalosophy;2324168]While my AS 3 is kind of flimsy what you would need to do is simply create a timer that would call the next array index that stores the XML data

  1. Load the XML
  2. Parse the XML and store into arrays
  3. Create function that shows index array position
  4. Create timer that call that function and increments position

Hope that helps.[/quote]

This is an image gallery I wrote a few weeks ago. Basically an AS 3 version of the kirupa gallery. Its pretty well commented so it should get you started.


package {
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.net.URLRequest;
	import flash.text.*;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.display.Sprite;
	import fl.controls.ComboBox;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;

	public class Gallery extends Sprite {
		trace("Loaded");
		// The variable to which the loaded XML will be assigned
		private var gallery:XML;
		// The object used to load the XML
		private var urlLoader:URLLoader;
		// Images array
		private var imgFiles:Array = [];
		// Image description array
		private var imgDesc:Array = [];
		// Player name array
		private var playName:Array = [];
		// Team array
		private var teamName:Array = [];
		// The asset loader
		private var loader:Loader;
		// Position of image
		private var p:Number;
		// Ratio of images
		private var ratio:Number;
		// Total records
		private var total:Number;

		// Constructor
		public function Gallery() {
			// Specify the location of the XML file
			var urlRequest:URLRequest = new URLRequest("xml/gallery.xml");
			// Create an object that can load external data
			urlLoader = new URLLoader();
			// Register to be notified when the XML file is finished loading
			urlLoader.addEventListener(Event.COMPLETE, completeListener);
			urlLoader.load(urlRequest);
		}
		public function TimerExample() {
			var myTimer:Timer = new Timer(1750, total -1);
			myTimer.addEventListener("timer", timerHandler);
			myTimer.start();
		}
		public function timerHandler(e:TimerEvent):void {
			trace("timerHandler: " + e);
			nextImage(e);
		}

		private function completeListener(e:Event):void {
			gallery = new XML(urlLoader.data);
			for each (var image:XML in gallery..IMAGE) {
				imgFiles.push(image);
			}
			for each (var desc:XML in gallery..DESCRIPTION) {
				imgDesc.push(desc);
			}
			for each (var player:XML in gallery..PLAYER) {
				playName.push(player);

			}
			for each (var team:XML in gallery..@TEAM) {
				teamName.push(team);
			}

			for (var k:Number = 0; k < teamName.length; k++) {
				for (var i:Number=0; i< teamName.length; i++) {
					var j:Number = i+1;
					if (teamName* == teamName[j]) {
						teamName.splice(j, 1);

					}
				}
			}
			for (var s:Number = 0; s < teamName.length; s++) {
				teamCombo.addItem({label: teamName[s], data: teamName[s]});
			}

			/*
			
			for each (var team:XML in gallery.attribute(TEAM)){
			teamName.push(team);
			teamCombo.addItem({label:"1",data:"1"});
			trace("helo");
			}
			*/
			total = imgFiles.length;
			createLoader();
			loadFirstImage();
			TimerExample();
			// Next Button Listener
			nextBtn.addEventListener(MouseEvent.MOUSE_DOWN, nextImage);
			prevBtn.addEventListener(MouseEvent.MOUSE_DOWN, prevImage);
			nextBtn.buttonMode = true;
			prevBtn.buttonMode = true;
		}
		private function loadFirstImage():void {
			p = 0;
			// load the first image
			myload(new URLRequest("images/" + imgFiles[p]));
		}
		private function nextImage(e:Event) {
			if (p < total -1) {
				p++;
				removeChild(loader.content);
				createLoader();
				myload(new URLRequest("images/" + imgFiles[p]));

			} else {
				// make an error call here
			}
		}
		private function prevImage(e:Event) {
			if (p ==0) {
				// make an error call here
			} else {
				p--;
				removeChild(loader.content);
				createLoader();
				myload(new URLRequest("images/" + imgFiles[p]));
			}
		}
		private function createLoader():void {
			// Create the loader
			loader = new Loader();
			// Register for events
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressListener);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,imgCompleteListener);
			loader.contentLoaderInfo.addEventListener(Event.INIT,initListener);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
		}
		private function myload(urlRequest:URLRequest):void {
			loader.load(urlRequest);
		}
		private function progressListener(e:ProgressEvent):void {
			statusText.text = "Loading: " + Math.floor(e.bytesLoaded / 1024) + "/" + Math.floor(e.bytesTotal / 1024) + " KB";
		}
		private function initListener(e:Event):void {
			addChild(loader.content);
		}
		private function imgCompleteListener(e:Event):void {
			if (loader.content.height > loaderBg.height) {
				ratio = loaderBg.height/loader.content.height;
				loader.content.height = loaderBg.height;
				loader.content.width *= ratio;
			}
			if (loader.content.width > loaderBg.width) {
				ratio = loaderBg.width/loader.content.width;
				loader.content.width = loaderBg.width;
				loader.content.height *= ratio;
			}
			loader.content.x = loaderBg.x;
			loader.content.y = loaderBg.y;
			loader.content.x = loaderBg.x+(loaderBg.width/2)-(loader.content.width/2);
			loader.content.y = loaderBg.y+(loaderBg.height/2)-(loader.content.height/2);
			var myTween:Tween = new Tween(loader.content, "alpha", Regular.easeIn, 0, 100, 2.2, true);
			statusText.text = imgDesc[p];
			currPlayerText.text = playName[p];
		}
		private function ioErrorListener(e:IOErrorEvent):void {
			statusText.text = "Load Error";
		}
	}
}

Thank you for this. I will definitely look at it and see if I can understand it. I do have one follow up question. I am unsure how packages work. You cannot enter them into the actions area like regular actionscript. How do i call those packages?

What you would need to do is create an .as file with the code above. Name it Gallery.as

Then create a .fla file and name it whatever you would like. Save it in the same directory as your Gallery.as file. Open your properties panel and look for where it says document class. Enter Gallery.as and click enter. Save and test your movie. You should then be linked to your class file.

:slight_smile:

I did what you said but noticed that you have put paths to XML and image files. Also I am unsure how to code for adding packages. Would I just put

import Gallery;

on the first line of frame 1 of my fla?

Also what is the structure of the XML file that you are using for this example?

[quote=Digitalosophy;2324691]What you would need to do is create an .as file with the code above. Name it Gallery.as

Then create a .fla file and name it whatever you would like. Save it in the same directory as your Gallery.as file. Open your properties panel and look for where it says document class. Enter Gallery.as and click enter. Save and test your movie. You should then be linked to your class file.

:)[/quote]

I’m at work so I don’t have access to the XML. I think your still a little confused about the packaging. I’ll zip everything (xml, images, fla, and class file) tonight for you.

Thanks I cannot wait to see it.

[quote=Digitalosophy;2325040]I’m at work so I don’t have access to the XML. I think your still a little confused about the packaging. I’ll zip everything (xml, images, fla, and class file)
tonight for you.[/quote]

Crap i forgot lol.

Hi… I took approach on the same lines but in simple way… if it is exactly that what you wanted…then I will be awarded.

Here is the script that you need to put inside fla file on the first frame…

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE,LoadXML);
xmlLoader.load(new URLRequest(“MLB.xml”));

var dataValue:Array = new Array();
var intCountTimerNumb:Number;
var datalen:Number;
var i:Number ;
var count:Number;
var timecount:Number;
function LoadXML(e:Event):void {

xmlData = new XML(e.target.data);
Parselist(xmlData);

}

function Parselist(MLBInput:XML):void {

datalen = MLBInput.Team.length();
intCountTimerNumb = Math.round(datalen/2);

//trace(datalen);
for(var i = 0; i<datalen ; i++){
dataValue* = MLBInput.Team.TeamName*.text()+ ’ ’ + MLBInput.Team.Scores*.text();
}

ShortTimer() ;
}

function ShortTimer()
{
// creates a new five-second Timer

       var minuteTimer:Timer = new Timer(1000, intCountTimerNumb);
        
        // designates listeners for the interval and completion events
        minuteTimer.addEventListener(TimerEvent.TIMER, onTick);
        minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
        
        // starts the timer ticking
       minuteTimer.start();
        trace("Team" +"	" + "Score");
    }

 function onTick(event:TimerEvent):void 
    {
        // displays the tick count so far
        // The target of this event is the Timer instance itself.
        //trace("tick " + event.target.currentCount);
        var count1:Number = event.target.currentCount-1;
        count = count1*2;
        trace(count);
        trace(datalen);
        var strLastData:String = dataValue[count+1];
        //trace(strLastData);
        if(strLastData == null)
        {
            trace(dataValue[count]);
        }
        else
        {
        trace(dataValue[count]+ "

" + dataValue[count+1]);
trace(" ");
}

        //trace(dataValue[count+1]);
        
        
    }
  function onTimerComplete(event:TimerEvent):void
    {
        trace("That's all!");
        
    }

function showdata(){

trace(dataValue);
trace(i);
}

Then your xml file in which I have just added couple of more nodes, will look like this:

<NHL>
<Team Host=“Visiting”>
<TeamName>Detroit</TeamName>
<Scores>4</Scores>
</Team>
<Team Host=“Visiting”>
<TeamName>Pittsburgh</TeamName>
<Scores>4</Scores>
</Team>
<Team Host=“Visiting”>
<TeamName>Philadelphia</TeamName>
<Scores>3</Scores>
</Team>
<Team Host=“Visiting”>
<TeamName>Xentaqsys</TeamName>
<Scores>10</Scores>
</Team>
<Team Host=“Visiting”>
<TeamName>Xentaqsys-SDS</TeamName>
<Scores>12</Scores>
</Team>
<Team Host=“Visiting”>
<TeamName>Xentaqsys-PLA</TeamName>
<Scores>1</Scores>
</Team>
<Team Host=“Visiting”>
<TeamName>Xentaqsys-SM</TeamName>
<Scores>Highest Scorer</Scores>
</Team>
</NHL>

You can add or Delete any node.

and yes you can see the result in output window only, not in swf, as of now. If it is helps you then please revert back to me for any queries.