Jump to content
Search Community

Loading of nodes into variables

AS3_Learning_Curve test
Moderator Tag

Recommended Posts

I'm just testing out loadermax and I can get images loading no problem at all. I wish to load some nodes from an xml file into memory and assign the values of the xml file into variables. This is what I have so far.

 

//create a LoaderMax named "mainQueue" and set up onProgress, onComplete and onError listeners
var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});

//append several loaders
queue.append( new XMLLoader("planets.xml", {name:"xmlDoc", noCache:1}) );

//start loading
queue.load();

function progressHandler(event:LoaderEvent):void {
   trace("progress: " + event.target.progress);
}

function completeHandler(event:LoaderEvent):void {

 trace(event.target + " is complete!");
}

 

I can get the xml file to load and the progress handler says it has loaded fine. Now here comes the million dollar question. Home do I then load the access the loaded xml file to put a node into a variable. Lets say for instance that I wish to put the node 250.0 into avariable distance. Sorry to ask such fundamental question but this would allow a me to continue with my project. As you can guess I wish to populate some variables with distances of planets etc. A simple example would be great with how to access the node within the xml and trace out the value.

 

Any input would be great. Thanks people.

 

 

This is a sample of my xml file named planets.xml

 

<?xml version="1.0" encoding="UTF-8" ?> 



Test




Xtml




Mars
250.0
Miles






 

Many thanks.

Link to comment
Share on other sites

what you need to do is a bit of reading on E4X which is what AS3 uses to search and access any type of xml data.

 

I created an xml file that looks like this:

 





Earth
1



mars
1.03



jupiter
.41


 

 

and I built a small example that illustrates the various ways of parsing that data:


import com.greensock.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.XMLLoader;

var myPlanets:XMLLoader = new XMLLoader("planetData.xml", {onComplete:getXML});

myPlanets.load();


function getXML(e:LoaderEvent):void{
var loadedXML:XML = myPlanets.content as XML;

trace("this is all the xml");
trace(loadedXML);

//create an XMLList of just the planet nodes
var planetsXML:XMLList = loadedXML..planet;



trace("\n\nplanets XMLList");
trace(planetsXML);

trace("\n\nall the name nodes")
trace(planetsXML.name);

trace("\n\nthe name of the first planet")
trace(planetsXML[0].name);

trace("\n\nthe rotation of the second planet")
trace(planetsXML[1].rotation);


trace("\n\nthere are this many planets");
trace(planetsXML.length());

trace("\n\nloop through the name and rotation of all planets")
for(var i:uint = 0; i		trace(planetsXML[i].name + " : " + planetsXML[i].rotation);
	} 


trace("\n\nget jupiter's rotation");
trace(planetsXML.(name=="jupiter").rotation);


}	

 

 

the above code will output the following:

this is all the xml



Earth
1


mars
1.03


jupiter
.41





planets XMLList

Earth
1


mars
1.03


jupiter
.41



all the name nodes
Earth
mars
jupiter


the name of the first planet
Earth


the rotation of the second planet
1.03


there are this many planets
3


loop through the name and rotation of all planets
Earth : 1
mars : 1.03
jupiter : .41


get jupiter's rotation
.41

 

I strongly recommend reading this E4X Tutorial http://www.republicofcode.com/tutorials/flash/as3xml/

 

...ignore the loading part as XMLLoader makes it much easier, but do read the "processing the xml" section.

Link to comment
Share on other sites

oh and for your example to get the value inside of: 250.0 just do

 

function completeHandler(event:LoaderEvent):void {

 trace(event.target + " is complete!");

 var planetsXML:XML = LoaderMax.getContent("planets.xml");

 trace(planetsXML..item[0].valuedec);
}	

Link to comment
Share on other sites

Carl,

 

I thank you for your time and efforts in making it clearer to me many thanks.

 

If I wished to have the following in the xml

Earth

1

 

Star1

150

 

Could you please let me know the correct way to call group 1 star span figure.

 

Would it be trace(planetsXML..group1.item[0].span);

 

Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...