Return to Kroll Design home page

Flash Tutorials (with .FLA files included)

 
« Return

ActionScript 3

XML example

Here's an introductory example using XML. If you click inside the Flash movie you can use page up / down or arrow up / down keys (or dragging the mouse) to scroll).
More to follow...

Download the FLA file



From the XML file:

<?xml version="1.0" encoding="UTF-8" ?>
<MyXMLData>
<!-- INTRODUCTORY COMMENTS: ------------------------------------------>
<introductory_comment>This is a sample XML file.</introductory_comment>
<!-- COLORS: --------------------------------------------------------->
<colors>	
  <color artist_primary="n" crt_primary="n" printer_primary="y">black</color>
  <color artist_primary="y" crt_primary="y" printer_primary="n">blue</color>
  <color artist_primary="n" crt_primary="n" printer_primary="y">cyan</color>
  <color artist_primary="n" crt_primary="y" printer_primary="n">green</color>
  <color artist_primary="y" crt_primary="y" printer_primary="y">green</color>
  <color artist_primary="n" crt_primary="n" printer_primary="y">magenta</color>
  <color artist_primary="n" crt_primary="n" printer_primary="n">orange</color>
  <color artist_primary="y" crt_primary="y" printer_primary="n">red</color>
  <color artist_primary="n" crt_primary="n" printer_primary="n">violet</color>
  <color artist_primary="n" crt_primary="n" printer_primary="n">white</color>
  <color artist_primary="y" crt_primary="y" printer_primary="y">yellow</color>
</colors>
<!-- SHAPES: --------------------------------------------------------->
<shapes>
  <shape>circle</shape>
  <shape>ellipse</shape>
  <shape>hexagon</shape>
  <shape>pentagon</shape>
  <shape>rectangle</shape>
  <shape>square</shape>
  <shape>triangle</shape>
</shapes>
</MyXMLData>

From the Action Script:

//------------------------------------------------------------
// Define a text field for displaying the XML's data.
// The way that the text field is defined here, a vertical scroll bar
// does not appear, but you can nevertheless scroll down using down
// arrow key, page down key, or mouse wheel:

   var myTextField:TextField = new TextField();
   myTextField.width = 400;
   myTextField.border = true;
	 myTextField.background = true;
	 myTextField.backgroundColor = 0xffffff;
   myTextField.height = 300;
	 myTextField.x = 20;
	 myTextField.y = 20;
   myTextField.wordWrap = true;
   myTextField.multiline = true;	 
	 myTextField.mouseWheelEnabled = true;
   addChild(myTextField);


//------------------------------------------------------------
// Indicate the location of the external XML file:

   var myXMLFile:URLRequest = new URLRequest("as3_myxml1.xml");

	 
//------------------------------------------------------------
// Load the external XML file into Flash:

   var myXMLLoader:URLLoader = new URLLoader();
   myXMLLoader.load(myXMLFile);

// This does the same thing in one line of code: 
// var myXMLLoader:URLLoader = new URLLoader(myXMLFile);

   myXMLLoader.addEventListener(Event.COMPLETE, runThisAfterXMLLoads);


//------------------------------------------------------------
// After the XML file is done loading, display data from it: 

function runThisAfterXMLLoads(myEvent:Event)
{
	var myXML = XML(myEvent.target.data);

	myTextField.text = myXML.introductory_comment + "\n";	
	myTextField.text = myXML.image.photographer + "\n"; 
	myTextField.appendText(myXML.colors.color[1] + "\n"); 
	myTextField.appendText(myXML.colors.color[1].@artist_primary + "\n"); 
	
	
//------------------------------------------------------------
// Loop through and display each element: 
   			
	 for each(var elements:XML in myXML.*)
	 {
		 if(elements.*.length() <= 1) // for top level elements
		 {
			 myTextField.appendText("type 1: " + elements.name() + ": " + elements + "\n");
		 }
		 else // for elements within other elements ("level 2" - Won't work for "level 3") 
		 {
			for each(var info:XML in elements.*)
			{
				myTextField.appendText("type 2: " + info.name() + ": " + info + "\n");
			}
		}
	 }
	 
} // end of function runThisAfterXMLLoads


 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM