Flash XML – Creating XML inside Flash

Posted on: February 28th, 2013 by tdc_user
This tutorial will demonstrate how to create XML inside Flash using AS3. Exercise 1: This exercise shows how you can generate a simple XML value in your Output box.
    First, if you start a new Flash session, create a new file, and make sure to choose AS3 as your actionscript setting. Hit F9 and you will be in the Actions code editor. Enter the following text:
var myXML:XML = <name>Brad</name>; trace(myXML);
When you execute this statement (by pressing CTRL + Enter on a PC or CMD + Enter on a Mac), you will get “Brad” in your Output box. Trace is a great feature for testing your programs since it writes whatever you “trace” to the Output box.
Exercise 2: This shows how we can output our values to a Combo box instead of just to the Output box.
    Start a new Flash file, again using AS3. Drag a Combo Box component to your Library. Drag a TextInput box from the Library to the Stage and name the instance info_txt. Create a new layer and name it a (as in “a”). Click on the first frame, press the F9 key, and start typing the following actionscript:
import fl.controls.*; var image:XML = new XML(<image/>); //image:XML – variable with an image datatype //<image/>creates an image element – //this is the shorthand method to open and close an element at the same time image.filename = “sagrada.jpg”; image.architect = “Antonio Gaudi”; trace(image); //sends result to output
The result you should now see in your Output box is this: <image> <filename>sagrada.jpg</filename> <architect>Antonio Gaudi</architect> </image> If we also want to see the result in the TextInput box, we can do so by adding this line:
info_txt.text = image;

Exercise 3: This shows how to pull specific information from the XML file and display it.
import fl.controls.*; var image:XML = new XML(<image/>); image.fileName = “sagrada.jpg”; image.architect = “Antonio Gaudi”; info_txt.text = image.architect;
This code will return the following content to your TextInput box: Antonio Gaudi (This happens since you requested the value “architect” from “image”.) Note: You may need to open the Properties panel and adjust the size of your TextInput box so it is big enough to show any output generated.
Exercise 4: This shows how to add an attribute to your XML code.
import fl.controls.*; var image:XML = new XML(<image/>); image.@fileName = “sagrada.jpg”; image.architect = “Antonio Gaudi”; info_txt.text = image;
This should return: <image filename=”sagrada.jpg”> <architect>Antonio Gaudi<architect> </image>
Challenge: Write code for a guitar xml file that returns this input to the info_txt.text box: <guitar filename=”R4001.jpg”> <brand>Rickenbacker</brand> <model>4001</model> </guitar> The answer to this challenge will be posted next week. Next Up: Importing XML Data with Flash. Tags: , ,

Comments are closed.