Solved

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

Posted on 2009-07-01
4
485 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…

707 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

15 Experts available now in Live!

Get 1:1 Help Now