Loading XML in ActionScript 3.0

My first thought was to see whether I could make use of XPath4AS2, but adapt it for use in ActionScript 3.0. I have no idea how much work that might involve. If all it means is wrapping the classes in a package code block, that seems easy enough, I suppose. Anything beyond that, and I think I’ll wait until a future release. However, I was unsure about how to add a classpath to Flash, since I thought I’d like to try using Flash instead of the mxmlc compiler that I have been using with the Flex 3 SDK.

It took a bit of trial and error, since I didn’t understand what the path was meant to define. I thought at first it was the full path to the class(es) that I wanted to use. Then it dawned on me that it was the path to the directory in which the classes are stored. None of the documentation I came across really spelled this out clearly. I decided to place all my classes here:


I discovered that the class path needs to be either relative to the directory in which the Flash document is stored, or absolute. The UNIX convention of using the tilde character to denote the user directory does not work when defining the classpath. I ended up setting the global classpath in Flash by going to Flash : Preferences : Category : ActionScript : ActionScript 3.0 Settings : Classpath.


Then, I can import classes relative to this path for any FLA files I create. So, the classes I wanted to import are stored here:


Now it makes total sense when I import classes using the following code in ActionScript:

import com.xfactorstudio.xml.xpath.XPath;

At any rate, that is all a side note to what I wanted to accomplish, which was to be able to load an external XML file and parse the contents. It took a bit of fiddling around with the code examples I had on hand to figure out how to use the XML once I had loaded it into an object. I wanted to find out whether I could load the file and output the contents to a text box. Here’s how I did it:

package {
	import flash.display.*;
	import flash.events.*; 
	import flash.net.*;
	import flash.text.*;

	public class XMLData extends Sprite {
		public var xmlString:String;
		public var xmlURL:String = "Products.xml";
		public var xmlData:XML;
		private var urlLoader:URLLoader;
		// constructor
		public function XMLData () {
			var urlRequest:URLRequest = new URLRequest(xmlURL);
			urlLoader = new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE, completeListener);

		// event listener
		private function completeListener(e:Event):void {
			xmlData = new XML(urlLoader.data);
			xmlString = xmlData.toXMLString();
			// display XML in a text box on the stage
		// create text box to display XML on stage
		public function addTextBox(string:String):void {
			var string:String;
			var textBox:TextField = new TextField();
			var format:TextFormat = new TextFormat();
			format.font = "Arial";
			format.size = 15;
			format.color = 0x000000;

			textBox.text = string;
			textBox.autoSize = TextFieldAutoSize.LEFT;

About this entry