Parsing XML using Flash and ActionScript 2.0
This is my first attempt at parsing XML using ActionScript. The stuff between the <pre> and </pre> tags is released to the public domain. Do what you will with it. Be aware though, that I can’t offer technical support … mostly because I don’t know enough to help you.
First, the XML:
<alldots> <dotname id="bigDot" color="0xff0000" url="http://www.fletchermartin.com/" photos="8" /> <dotname id="otherDot" color="0x000066" url="http://www.ajc.com/" photos="8" /> <dotname id="thirdDot" color="0xCC0099" url="http://www.tiffanybbrown.com/" photos="0" /> </alldots>
Then the ActionScript (commented for your learning pleasure):
var dots:XML = new XML();
dots.load('bigdot.xml');
dots.onLoad = function(success:Boolean){
if(success){
if(dots.status == 0){
var dotsToXMLString:String = new String(); // initializes a new string variable
dotsToXMLString = dots.toString(); // converts dots XML object to a string and stores it in dotsToXMLString.
var dotsXML:XML = new XML(dotsToXMLString);// creates new XML object with the string contents from above.
dotsXML.parseXML(dotsToXMLString); // parses the string from above.
var dotsNodes:Object = dotsXML.firstChild; // Saves the firstChild (in this case, the outermost element) as an object
var dotsNodesChildren:Object = dotsNodes.childNodes; // Saves the childNodes of firstChild as an object
for(i=0;i<dotsNodesChildren.length;i++){
var newObj:Object = dotsNodes.childNodes[i].attributes.id; // creates a new object out of the child node's id.
var newObjColor:Color = new Color(newObj); // creates a new color object with newObj as its target
var theColor:Number = dotsNodes.childNodes[i].attributes.color; //retrieves the hex code value (number) of the attribute color
newObjColor.setRGB(theColor); // sets the RGB value of newObjColor.
}
} else {
trace("Problem parsing XML.");
}
} else{
trace("Could not load XML");
}
}
View everything in action, or download the .fla, and XML files.