Solved

AS3/ XML Loop through a group of parsed xml paths and update them in the same textfields.

Posted on 2009-07-01
4
487 Views
Last Modified: 2013-11-11
With the following code I have several dynamic textfields that have an unchangeable xml path.  I what to be able to loop to the next node section of the xml and load them into the same textfields upon an update function.  Please look at mu code and see if this makes sense...

As you can see in the code I have several sections being updated, but what I would like to do is simplify this so that no matter how many xml nades are produced they will be updated into the same textfields (upon another function probably going to be a timer).

Also the: Flights.flight_mc.schedTime_txt.text = newTimeStr; paths will need to be updated as well, but they are unique as they have different variables associated with them.  I hope I havent made this too difficult to understand.  If so I would be glad to elaborate...  Just let me knoe what you need to know.

As always any help is greatly appreciated!
 
var xmlLoader:URLLoader = new URLLoader();

var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("http://www.flightstats.com/go/rss/flightStatusByAirport.do?guid=34b64945a69b9cac:28fe44e:122319722a2:-1e56&airportCode=TUL&airportQueryType=0&sortField=0&codeshareDisplay=1"));

function LoadXML(e:Event):void {

	trace("LoadXML");

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

	ParseContent(xmlData);

}

function ParseContent(contentInput:XML):void {

	Refresh();

	namespace someNamespace= "http://rss.flightstats.com/ns/rss/1.0"

	var timeStr:String = contentInput.channel.item.someNamespace::FlightHistory[0].@DepartureDate;

	var dt:String = timeStr.split(" ")[0];

	var hr:String = timeStr.split(" ")[1].split(":")[0];

	var mn:String = timeStr.split(" ")[1].split(":")[1];

	var ampm:String = "A";

	var i:uint;

	for (i=0; i<myArray.length; i++) {

		// code

	}

	if (parseInt(hr) > 12) {

		ampm = "P";

		hr = (parseInt(hr) - 12).toString();

	}

	var newTimeStr:String = hr + ":" + mn + ampm;

	trace("newTimeStr = " + newTimeStr);// newTimeStr = 2009-06-30 3:00 PM

	var timeStr2:String = contentInput.channel.item.someNamespace::FlightHistory[0].@EstimatedGateDepartureDate;

	var dt2:String = timeStr2.split(" ")[0];

	var hr2:String = timeStr2.split(" ")[1].split(":")[0];

	var mn2:String = timeStr2.split(" ")[1].split(":")[1];

	var ampm2:String = "A";
 

	if (parseInt(hr2) > 12) {

		ampm2 = "P";

		hr2 = (parseInt(hr2) - 12).toString();

	}

	var newTimeStr2:String = hr + ":" + mn + ampm;

	trace("newTimeStr2 = " + newTimeStr2);

	

	Flights.flight_mc.airline_txt.text = contentInput.channel.item.someNamespace::FlightHistory[0].someNamespace::Airline.@Name;

	Flights.flight_mc.location_txt.text = contentInput.channel.item.someNamespace::FlightHistory[0].someNamespace::Destination.@City;

	Flights.flight_mc.gate_txt.text = contentInput.channel.item.someNamespace::FlightHistory[0].@DepartureGate;

	Flights.flight_mc.schedTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[0].@DepartureDate;

	Flights.flight_mc.schedTime_txt.text = newTimeStr;

	Flights.flight_mc.estTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[0].@EstimatedGateDepartureDate;

	Flights.flight_mc.estTime_txt.text = newTimeStr2;

	Flights.flight_mc.status_txt.text = contentInput.channel.item.someNamespace::FlightHistory[0].@Status;
 

	Flights.flight_mc2.airline_txt.text = contentInput.channel.item.someNamespace::FlightHistory[1].someNamespace::Airline.@Name;

	Flights.flight_mc2.location_txt.text = contentInput.channel.item.someNamespace::FlightHistory[1].someNamespace::Destination.@City;

	Flights.flight_mc2.gate_txt.text = contentInput.channel.item.someNamespace::FlightHistory[1].@DepartureGate;

	Flights.flight_mc2.schedTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[1].@DepartureDate;

	Flights.flight_mc2.estTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[1].@EstimatedGateDepartureDate;

	Flights.flight_mc2.status_txt.text = contentInput.channel.item.someNamespace::FlightHistory[1].@Status;
 

	Flights.flight_mc3.airline_txt.text = contentInput.channel.item.someNamespace::FlightHistory[2].someNamespace::Airline.@Name;

	Flights.flight_mc3.location_txt.text = contentInput.channel.item.someNamespace::FlightHistory[2].someNamespace::Destination.@City;

	Flights.flight_mc3.gate_txt.text = contentInput.channel.item.someNamespace::FlightHistory[2].@DepartureGate;

	Flights.flight_mc3.schedTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[2].@DepartureDate;

	Flights.flight_mc3.estTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[2].@EstimatedGateDepartureDate;

	Flights.flight_mc3.status_txt.text = contentInput.channel.item.someNamespace::FlightHistory[2].@Status;

	

        Flights.flight_mc4.airline_txt.text = contentInput.channel.item.someNamespace::FlightHistory[3].someNamespace::Airline.@Name;

	Flights.flight_mc4.location_txt.text = contentInput.channel.item.someNamespace::FlightHistory[3].someNamespace::Destination.@City;

	Flights.flight_mc4.gate_txt.text = contentInput.channel.item.someNamespace::FlightHistory[3].@DepartureGate;

	Flights.flight_mc4.schedTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[3].@DepartureDate;

	Flights.flight_mc4.estTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[3].@EstimatedGateDepartureDate;

	Flights.flight_mc4.status_txt.text = contentInput.channel.item.someNamespace::FlightHistory[3].@Status;

	

        Flights.flight_mc5.airline_txt.text = contentInput.channel.item.someNamespace::FlightHistory[4].someNamespace::Airline.@Name;

	Flights.flight_mc5.location_txt.text = contentInput.channel.item.someNamespace::FlightHistory[4].someNamespace::Destination.@City;

	Flights.flight_mc5.gate_txt.text = contentInput.channel.item.someNamespace::FlightHistory[4].@DepartureGate;

	Flights.flight_mc5.schedTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[4].@DepartureDate;

	Flights.flight_mc5.estTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[4].@EstimatedGateDepartureDate;

	Flights.flight_mc5.status_txt.text = contentInput.channel.item.someNamespace::FlightHistory[4].@Status;

}

Open in new window

0
Comment
Question by:cubical38
  • 2
  • 2
4 Comments
 

Author Comment

by:cubical38
ID: 24764829
Okay here is what I have thus far which is working, sort of...  The problem:  once the first 5 nodes have been loaded, how do I increment through to the next five and so on and so on...  Also along with that how do I increment through my newTimeStr variable?  Hope this makes it a clearer on what I am trying to accomplish...

so to recap:  there are five movieclips each containing 6 dynamic textfields.  Each movieclip will load in the next specified xml node and its corresponding text.  Once I reach the end of those 5 nodes how do I increment it so that on a function such as click to next it loads the next 5 nodes into the same movieclip textfields?

Thanks Again, and any direction is helpful!


       
 
var xmlLoader:URLLoader = new URLLoader();

var xmlData:XML = new XML();

var xmlList:XMLList = new XMLList();

var index:Number = 0;

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("http://www.flightstats.com/go/rss/flightStatusByAirport.do?guid=34b64945a69b9cac:28fe44e:122319722a2:-1e56&airportCode=TUL&airportQueryType=0&sortField=0&codeshareDisplay=1"));

function LoadXML(e:Event):void {

	trace("LoadXML");

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

	xmlList = xmlData.children();

	trace(xmlList);

	ParseContent(xmlData);

}
 

function ParseContent(contentInput:XML):void {

	Refresh();

	namespace someNamespace= "http://rss.flightstats.com/ns/rss/1.0"
 

	var timeStr:String = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureDate;

	var dt:String = timeStr.split(" ")[0];

	var hr:String = timeStr.split(" ")[1].split(":")[0];

	var mn:String = timeStr.split(" ")[1].split(":")[1];

	var ampm:String = "A";
 

	if (parseInt(hr) > 12) {

		ampm = "P";

		hr = (parseInt(hr) - 12).toString();

	}

	var newTimeStr:String = hr + ":" + mn + ampm;

	trace("newTimeStr = " + newTimeStr);// newTimeStr = 2009-06-30 3:00 PM

	var timeStr2:String = contentInput.channel.item.someNamespace::FlightHistory[index].@EstimatedGateDepartureDate;

	

	var dt2:String = timeStr2.split(" ")[0];

	var hr2:String = timeStr2.split(" ")[1].split(":")[0];

	var mn2:String = timeStr2.split(" ")[1].split(":")[1];

	var ampm2:String = "A";
 

	if (parseInt(hr2) > 12) {

		ampm2 = "P";

		hr2 = (parseInt(hr2) - 12).toString();

	}

	var newTimeStr2:String = hr + ":" + mn + ampm;

	trace("newTimeStr2 = " + newTimeStr2);
 

	Flights.flight_mc.airline_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].someNamespace::Airline.@Name;

	Flights.flight_mc.location_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].someNamespace::Destination.@City;

	Flights.flight_mc.gate_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureGate;

	Flights.flight_mc.schedTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureDate;

	Flights.flight_mc.schedTime_txt.text = newTimeStr;

//not sure how to increment this variable

	Flights.flight_mc.estTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@EstimatedGateDepartureDate;

	Flights.flight_mc.estTime_txt.text = newTimeStr2;

	Flights.flight_mc.status_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@Status;

	index ++;

	Flights.flight_mc2.airline_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].someNamespace::Airline.@Name;

	Flights.flight_mc2.location_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].someNamespace::Destination.@City;

	Flights.flight_mc2.gate_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureGate;

	Flights.flight_mc2.schedTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureDate;

	Flights.flight_mc.schedTime_txt.text = newTimeStr;

	Flights.flight_mc2.estTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@EstimatedGateDepartureDate;

	Flights.flight_mc.estTime_txt.text = newTimeStr2;

	Flights.flight_mc2.status_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@Status;

	index ++;

	Flights.flight_mc3.airline_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].someNamespace::Airline.@Name;

	Flights.flight_mc3.location_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].someNamespace::Destination.@City;

	Flights.flight_mc3.gate_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureGate;

	Flights.flight_mc3.schedTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureDate;

	Flights.flight_mc.schedTime_txt.text = newTimeStr;

	Flights.flight_mc3.estTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@EstimatedGateDepartureDate;

	Flights.flight_mc.schedTime_txt.text = newTimeStr2;

	Flights.flight_mc3.status_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@Status;

	index ++;

	Flights.flight_mc4.airline_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].someNamespace::Airline.@Name;

	Flights.flight_mc4.location_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].someNamespace::Destination.@City;

	Flights.flight_mc4.gate_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureGate;

	Flights.flight_mc4.schedTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureDate;

	Flights.flight_mc.schedTime_txt.text = newTimeStr;

	Flights.flight_mc4.estTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@EstimatedGateDepartureDate;

	Flights.flight_mc.schedTime_txt.text = newTimeStr2;

	Flights.flight_mc4.status_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@Status;

	index ++;

	Flights.flight_mc5.airline_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].someNamespace::Airline.@Name;

	Flights.flight_mc5.location_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].someNamespace::Destination.@City;

	Flights.flight_mc5.gate_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureGate;

	Flights.flight_mc5.schedTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureDate;

	Flights.flight_mc.schedTime_txt.text = newTimeStr;

	Flights.flight_mc5.estTime_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@EstimatedGateDepartureDate;

	Flights.flight_mc.schedTime_txt.text = newTimeStr2;

	Flights.flight_mc5.status_txt.text = contentInput.channel.item.someNamespace::FlightHistory[index].@Status;

//at this point I need to upon a function or timer reload these same textfields with the next set of xml nodes

}

Open in new window

0
 
LVL 22

Accepted Solution

by:
rascalpants earned 500 total points
ID: 24768335
well, this looks like a great place to introduce a for loop, and populate the movieclip text fields with it...

and get everything out of the XML event handler... it should be in a seperate function.

also, if you are going to use a sequentially numbered movieclip structure, then don't start with a generic clip name...

so..  "flight_mc"  should actually be "flight_mc1"

change that and make this easier on everyone...   :)


rp


//

var clips:Array = new Array();

clips.push( {clip: Flights.flight_mc1} );

clips.push( {clip: Flights.flight_mc2} );

clips.push( {clip: Flights.flight_mc3} );

clips.push( {clip: Flights.flight_mc4} );

clips.push( {clip: Flights.flight_mc5} );

//

var currIndex:int = 0;

//
 

var xmlLoader:URLLoader = new URLLoader();

var xmlData:XML = new XML();

var xmlList:XMLList = new XMLList();

var index:Number = 0;

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("http://www.flightstats.com/go/rss/flightStatusByAirport.do?guid=34b64945a69b9cac:28fe44e:122319722a2:-1e56&airportCode=TUL&airportQueryType=0&sortField=0&codeshareDisplay=1"));

function LoadXML(e:Event):void {

	trace("LoadXML");

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

	xmlList = xmlData.children();

	trace(xmlList);

	ParseContent(xmlData);

}

 

function ParseContent(contentInput:XML):void {

	Refresh();

	namespace someNamespace= "http://rss.flightstats.com/ns/rss/1.0"

 

	var timeStr:String = contentInput.channel.item.someNamespace::FlightHistory[index].@DepartureDate;

	var dt:String = timeStr.split(" ")[0];

	var hr:String = timeStr.split(" ")[1].split(":")[0];

	var mn:String = timeStr.split(" ")[1].split(":")[1];

	var ampm:String = "A";

 

	if (parseInt(hr) > 12) {

		ampm = "P";

		hr = (parseInt(hr) - 12).toString();

	}

	var newTimeStr:String = hr + ":" + mn + ampm;

	trace("newTimeStr = " + newTimeStr);// newTimeStr = 2009-06-30 3:00 PM

	var timeStr2:String = contentInput.channel.item.someNamespace::FlightHistory[index].@EstimatedGateDepartureDate;

	

	var dt2:String = timeStr2.split(" ")[0];

	var hr2:String = timeStr2.split(" ")[1].split(":")[0];

	var mn2:String = timeStr2.split(" ")[1].split(":")[1];

	var ampm2:String = "A";

 

	if (parseInt(hr2) > 12) {

		ampm2 = "P";

		hr2 = (parseInt(hr2) - 12).toString();

	}

	var newTimeStr2:String = hr + ":" + mn + ampm;

	trace("newTimeStr2 = " + newTimeStr2);

     

    updateMovieClips( currIndex ); 

}
 

function updateMovieClips( currIndex:int ):void

{

  var len:int = clips.length;

  for(var i:int = 0; i < len; i++)

  {

    var theData:XML = contentInput.channel.item.someNamespace::FlightHistory[ currIndex ];

    

    clips[i].airline_txt.text = theData.someNamespace::Airline.@Name;

    clips[i].location_txt.text = theData.someNamespace::Destination.@City;

    clips[i].gate_txt.text = theData.@DepartureGate;

    clips[i].schedTime_txt.text = theData.@DepartureDate;

    clips[i].schedTime_txt.text = newTimeStr;

    clips[i].estTime_txt.text = theData.@EstimatedGateDepartureDate;

    clips[i].estTime_txt.text = newTimeStr2;

    clips[i].status_txt.text = theData.@Status;

  }
 

  currIndex++;

}

Open in new window

0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24768341
the above code might not compile properly, but I thought I would get you started before I left on holiday til monday...

rp
0
 

Author Closing Comment

by:cubical38
ID: 31598969
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xslt copy xml document for each occurance of a repeating node 4 13
Can't play streaming video on event.on24.com 7 198
json format text only 4 75
read an xml file in perl 2 15
Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now