Working with .XML files in Adobe Flash and AS3

XML is a structured, text-based way of formatting and describing data, using tags that are similar to HTML tags, but not predefined as HTML tags are.
Flash can read the file and convert it to an instance of the XML class, by providing to your XML file the correct structure. Also Flash allows you to read information in an XML file into an instance of the XML class, which has predefined methods and properties associated with it. If you are familiar with HTML, learning the basics of XML should not be difficult.

What is XML?

XML is a way of representing structured data, that you explicitly define the context for the data.

  • XML is composed mainly of nodes
  • Every XML must have a matching opening and closing tag
  • Elements can contain nested nodes (be they other elements or text nodes)
  • Using XML in Flash

    To read an XML file into an XML instance in Flash, you need to do:

  • create a new XML instance
  • create a URLRequest instance containing the XML data to send
  • use URLLoader to send the data and receive the response
  • display the content from XML
  • Create a new XML file, with any text editor:

    <?xml version="1.0" encoding="utf-8" ?>
    <!--your nodes here-->

    Now let’s add a simple node that will contain blank text:

    <?xml version="1.0" encoding="utf-8" ?>

    So far it’s a simple example, with one node that will contain text. If you want to use HTML or other text that uses characters with specific meaning in the context of XML you muse use a CDATA tag to enclose the text.

    <?xml version="1.0" encoding="utf-8" ?>
            <textnode><![CDATA[<a href="">This is a text sample</a>]]></textnode>

    The XML file is done, save this file as “example.xml” and  let’s read his content in Flash.

    Reading the content

    Create a new ActionScript File and save it as “” ( – this will be the main class for our flash file; the package classes extends the Sprite class (since we don’t have a timeline animation). Finally create a new AS3 Flash file (590×300px) and open it, so that we have both files opened.

    Main Movie

    On main movie create a simple text field with instance name “txt”, make this dynamic and select from the Character panel, option “Render text as HTML”. In Document Class put the path for the Main class: “com.fladev.xml.MainClass”. You don’t have to do anything else here, let’s skip to

    First we need to add some variables:

    private static const xmlFile:String = "example.xml"//XML url
    private var xml:XML;//create the XML
    private var urlLoader:URLLoader;//set the urlLoader for XML

    The constructor function will be like this:

    public function MainClass() {
    	urlLoader = new URLLoader();
    	urlLoader.load(new URLRequest(xmlFile));//request to the URL
    	urlLoader.addEventListener(Event.COMPLETE, showXML);//display the content after the XML is loaded
    private function showXML(e:Event):void
    	xml = new XML(;
    	txt.htmlText = xml.textnode;//the trick is that the textfield "txt" must be "htmlText" to display the special characters; "textnode" is the node from the XML file;

    Hope you learn something about XML files, it’s not so hard and you don’t have to be an expert. Just in case you need more info about this, just check this link

    Twitter Digg Delicious Stumbleupon Technorati Facebook

    2 Responses to “Working with .XML files in Adobe Flash and AS3”

    1. XML is a powerfull and flexible way to exchange data. I heard that JSON is much faster in exchange, although in my opinion XML is still ahead when it comes to handle more complex datastructures. What is your opinion on this. Do you know some test or have some experience with these two structures?