Tiffany B. Brown

A web log about web development and internet culture with frequent detours into other stuff.
Live redesign in stages: Reorganizing content
Seeking a new web-based RSS reader

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.

Share this entry:
  • Digg
  • Technorati
  • del.icio.us
  • Ma.gnolia
  • Mixx
  • NewsVine
  • Reddit
  • StumbleUpon
  • TailRank
  • Furl
  • Slashdot
  • Global Grind
  • YahooMyWeb
  • Facebook
  • Google
  • Live

One comment

  1. Hmm. Commenting for _our_ learning pleasure.. I thought whitespace and liberal comments was a generally accepted practice ;)

previous post: Live redesign in stages: Reorganizing content
next post: Seeking a new web-based RSS reader