Link to home
Start Free TrialLog in
Avatar of ofeargall
ofeargall

asked on

How do I capture array/xmlList information with AS3?

I'm loading and parsing an XML file with reference the SWF files. I can load an individual file to the stage but I don't know how to get the xml node information using AS3?

The goal is to create a 'Next' button and 'Back' button for loading the respective SWF files referenced in the xml.

My assumption is that when I get the node info from the item loaded then I can create a variable that is +1 and -1 and put that info into a button that will pass into a function, correct?


Thank you for any help you can provide.
var xml:XML;
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();
var fullLoader:Loader = new Loader();
 
nextBtn.addEventListener(MouseEvent.CLICK, showImage);
nextBtn.x = 300;
nextBtn.y = 300;
nextBtn.buttonMode = true;
 
xmlLoader.load(new URLRequest("data/modules.xml"));
 
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
 
function xmlLoaded(event:Event):void
{
	addChild(nextBtn);
}
 
 
function showImage(event:MouseEvent):void
{
	xml = XML(xmlLoader.data);
	xmlList = xml.children();
	fullLoader.unload();
	fullLoader.load(new URLRequest(xmlList[1].attribute("source")));
}

Open in new window

Avatar of blue-genie
blue-genie
Flag of South Africa image

what you can do is create a counter that will hold the current loaded file
then as you say when you click next + 1 and previous - 1, and get the node at that index.
if you need a specific code example you'll need to upload a sample of your xml file.
xml in as3 is treated like arrays.
for example...
xml data like this...

<test>
   <item name="aa" switch="on">http://www.google.com</item>
   <item name="bb" switch="off">http://www.yahoo.com</item>
   <item name="cc" switch="off">https://www.experts-exchange.com</item>
</test>

if u want to retrieve the value "aa", u do this...
xmlObj.item[0].@name

if u want to retrieve the value http://www.yahoo.com, u do this...
xmlObj.item[1]

if u want to retrieve the value of the switch in the the 3rd item, u do this...
xmlObj.item[2].@switch

xmlObj is a variable that stores ur loaded xml data.
got it?
Avatar of ofeargall
ofeargall

ASKER

I've tried to create an array that holds the data (new code attached..). When I trace it though, it crams all the data into one string.

Also attached is my xml (renamed as .txt).

Do I understand the logic correctly in that

1. the xml loads
2. loop through the data and load the nodes into an array
3. load the first node onto the stage
4. create a function for the "next" button that makes an integer
5. take the integer and call the node in the array that is one greater than the current node loaded on the stage

What is the method for calling that info from the array and figuring out what node it's node number is (index?)?

Or, do I skip all that and just pull the data from the xml as trypt suggests since it is already an array as an xml file?

I know that you are all working citizens so I appreciate your time and wisdom.

PS, I'm loading jpg images instead of swf files during this testing phase...


var xml:XML;
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();
var container:MovieClip = new MovieClip();
var imageLoader:Loader;
var nextBtn:mcNextBtn = new mcNextBtn();
var thumbs:Array = new Array();
var description:Array = new Array();
 
container.x = 25;
container.y = 25;
 
nextBtn.addEventListener(MouseEvent.CLICK, nextImage);
nextBtn.x = 300;
nextBtn.y = 300;
nextBtn.buttonMode = true;
 
xmlLoader.load(new URLRequest("data/modules.xml"));
 
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
 
function xmlLoaded(event:Event):void
{
	xml = XML(event.target.data)
	xmlList = xml.children();
	for(var i:int = 0; i < xmlList.length(); i++);
	{
		thumbs[i] = xmlList.attribute("thumbs");
		description[i] = xmlList.children();
	}
}
 
function nextImage(evet:MouseEvent):void
{
	
}
 
addChild(container);
addChild(nextBtn);

Open in new window

modules.txt
dude, u have to forget about how xml used to work in as2, it's a total different story!
there isn't any node or what as2 used to call it... so, please, erase that as2 from ur mind first!

u do not need to create an extra array to hold your xml data.
xml object behaves like an array!

1. the xml loads ... ok this is ok
2. loop through the data and load the nodes into an array ... no u don't need this step anymore!
3. load the first node onto the stage ...no u don't need this!
4. create a function for the "next" button that makes an integer ... nope
5. take the integer and call the node in the array that is one greater than the current node loaded on the stage ... nope

take some time and read this article, so u'll get familiar with xml in as3
http://www.kirupa.com/developer/flashcs3/using_xml_as3_pg1.htm
The attached code gives me a list of the urls to the images referenced in the xml - excellent! But, my question is still how to make a button to load one of the urls to the stage. More important, how do I load the item to the stage that is one url greater than the one already on stage?

I have the info, but how do I move it out of my function and into another?
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
var imageLoader:Loader
 
xmlLoader.addEventListener(Event.COMPLETE, loadXML);
xmlLoader.load(new URLRequest("data/modules.xml"));
 
function loadXML(event:Event):void
{
	xmlData = new XML(event.target.data);
	ParseModules(xmlData);
}
 
function ParseModules(moduleInput:XML):void
{
	trace("XML Output");
	trace("------------------------");
	var thumbsList:XMLList = moduleInput.module.attribute("source");
	for (var i:int = 0; i < thumbsList.length(); i++)
	{
		var moduleElement:XML = thumbsList[i];
		trace(moduleElement);
	}
}

Open in new window

blue-genie, how does one go about creating the counter. I've finally gotten my code revised to what I believe will work best for my application. It functions properly when I manually advance the 'array/node' number in the nextImage function. I just need to tell my nextImage function which node the fullLoader is displaying from the xml file so I can add 1 to that, does that sound correct?
var images_xml:XML;
var xmlReq:URLRequest = new URLRequest("data/modules.xml");
var xmlLoader:URLLoader = new URLLoader();
 
var nextBtn:mcNextBtn = new mcNextBtn();
var prevBtn:mcPrevBtn = new mcPrevBtn();
var fullLoader:Loader = new Loader();
 
prevBtn.visible = false;
nextBtn.x = 125;
nextBtn.y = 200;
nextBtn.addEventListener(MouseEvent.CLICK, nextImage);
 
 
function xmlLoaded(event:Event):void
{
	images_xml = new XML(xmlLoader.data);
	addChild(nextBtn);
	addChild(prevBtn);
	fullLoader.load(new URLRequest(images_xml.*[0].@source));
	//trace(images_xml.*[0 + 5].@source);
}
 
function nextImage(event:MouseEvent):void
{
	images_xml = new XML(xmlLoader.data);
	fullLoader.unload();
	fullLoader.load(new URLRequest(images_xml.*[0 + 1].@source));
}
addChild(fullLoader);
 
xmlLoader.load(xmlReq);
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of blue-genie
blue-genie
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
blue-genie,

When I use [p] doubles the first image but doesn't display the last image in the XML.
If I use [p +1] it works correctly. I'm guessing it's because I've already loaded the first image to the loader in the xmlLoaded function. Either way, you're right! The counter was the trick.

Thanks!
//this is the load function for the xml
function xmlLoaded(event:Event):void
{
	images_xml = new XML(xmlLoader.data);
	addChild(nextBtn);
	addChild(prevBtn);
	fullLoader.load(new URLRequest(images_xml.*[0].@source));
	//trace(images_xml.*[0 + 5].@source);
}
 
//this is after the load is done and the first image is in the loader
function nextImage(event:MouseEvent):void
{
	if (p < images_xml.module.length() - 1)
	{
		prevBtn.visible = true;
		nextBtn.visible = true;
		nextPic = images_xml.module[p + 1].@source; //Puts next XML item in "nextPic" variable
		fullLoader.unload();
		fullLoader.load(new URLRequest(nextPic));
		p++;
		if (p == images_xml.module.length() - 1)
		{
			nextBtn.visible = false;
		}
	}
}

Open in new window

Thanks for your help. I was able to combine your info with some other things I recently learned.