Home arrow Flash arrow Page 5 - Using XML and ActionScript with Flex Applications
FLASH

Using XML and ActionScript with Flex Applications


In this conclusion to a five-part series on using ActionScript to build sophisticated programs in Flex, we will add XML to the equation. It is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 3 stars3 stars3 stars3 stars3 stars / 20
May 29, 2008
TABLE OF CONTENTS:
  1. · Using XML and ActionScript with Flex Applications
  2. · Reading XML Data
  3. · Writing to and Editing XML Objects
  4. · Reflection
  5. · Getting the Class by Name

print this article
SEARCH DEVARTICLES

Using XML and ActionScript with Flex Applications - Getting the Class by Name
(Page 5 of 5 )

If you have a class name, you can retrieve a reference to the class using the getDefinitionByName() function. The function requires a string parameter specifying a class name, and it returns an Object type. The function returns an Object type rather than aClasstype because it could also theoretically return a reference to a function if you pass it a fully qualified function name (e.g.,flash.util.getTimer). If you’re certain that you’re retrieving a class reference, you can cast the return value toClass, as in the following example:

  var classReference:Class = Class(getDefinitionByName("flash.net.URLLoader"));

Once you’ve retrieved a reference to a class, you can create anewinstance, as follows:

  var instance:Object = new classReference();

Obviously you can use the return value fromgetQualifiedClassName()orgetQualifiedSuperclassName()in conjunction withgetDefinitionByName(), as in the following example:

  var loader:URLLoader = new URLLoader();
  var className:String = getQualifiedClassName(loader);
  var classReference:Class = Class(getDefinitionByName(className));
  var instance:Object = new classReference();

Class Introspection

You can use describeType() to return a description of all the events, public properties, and public methods of an object. Simply pass the method a reference to the object you want to introspect. The method returns an XML object that details the class name, superclass, various class settings, implemented interfaces, constructor signature, public method signatures, and public properties descriptions. 

The following example retrieves the description for aURLLoaderobject:

  var loader:URLLoader = new URLLoader();
  var description:XML = describeType(loader);
  trace(description);

The preceding example outputs the following:

  <type name="flash.net::URLLoader" base="flash.events::EventDispatcher"
  isDynamic="false" isFinal="false" isStatic="false">
   
<metadata name="Event">
      <arg key="name" value="httpStatus"/>
      <arg key="type" value="flash.events.HTTPStatusEvent"/>
    </metadata>
   
<metadata name="Event">
      <arg key="name" value="securityError"/>
      <arg key="type" value="flash.events.SecurityErrorEvent"/>
    </metadata>
   
<metadata name="Event">
      <arg key="name" value="ioError"/>
      <arg key="type" value="flash.events.IOErrorEvent"/>

    </metadata>
   
<metadata name="Event">
      <arg key="name" value="progress"/>
      <arg key="type" value="flash.events.ProgressEvent"/>
    </metadata>
   
<metadata name="Event">
      <arg key="name" value="complete"/>
      <arg key="type" value="flash.events.Event"/>
   
</metadata>
   
<metadata name="Event">
      <arg key="name" value="open"/>
      <arg key="type" value="flash.events.Event"/>
   
</metadata>
    <extendsClass type="flash.events::EventDispatcher"/>
    <extendsClass type="Object"/>
    <implementsInterface type="flash.events::IEventDispatcher"/>
    <constructor>
      <parameter index="1" type="flash.net::URLRequest" optional="true"/>
    </constructor>
    <variable name="bytesTotal" type="uint"/>
    <variable name="data" type="*"/>
    <method name="load" declaredBy="flash.net::URLLoader" returnType="void">
     
<parameter index="1" type="flash.net::URLRequest" optional="false"/>
    </method>
    <method name="close" declaredBy="flash.net::URLLoader" returnType="void"/>
    <variable name="dataFormat" type="String"/>
    <variable name="bytesLoaded" type="uint"/>
    <method name="dispatchEvent" declaredBy="flash.events::EventDispatcher"
  returnType="Boolean">
     
<parameter index="1" type="flash.events::Event" optional="false"/>
    </method>
    <method name="toString" declaredBy="flash.events::EventDispatcher"
  returnType="String"/>
   
<method name="willTrigger" declaredBy="flash.events::EventDispatcher"
  returnType="Boolean">
     
<parameter index="1" type="String" optional="false"/>
    </method>
    <method name="addEventListener" declaredBy="flash.events::EventDispatcher"
  returnType="void">
      <parameter index="1" type="String" optional="false"/>
      <parameter index="2" type="Function" optional="false"/>
      <parameter index="3" type="Boolean" optional="true"/>
      <parameter index="4" type="int" optional="true"/>
      <parameter index="5" type="Boolean" optional="true"/>
   
</method>
    <method name="hasEventListener" declaredBy="flash.events::EventDispatcher"
  returnType="Boolean">
     
<parameter index="1" type="String" optional="false"/>
    </method>
    <method name="removeEventListener" declaredBy="flash.events::EventDispatcher"
  returnType="void">
      <parameter index="1" type="String" optional="false"/>
      <parameter index="2" type="Function" optional="false"/>
      <parameter index="3" type="Boolean" optional="true"/>
   
</method>
  </type>

With some work, you can create complex systems that use objects to create sophisticated and dynamic applications.

Summary

In this chapter, we discussed the fundamentals of ActionScript 3.0. ActionScript is the ECMAScript-standard-based programming language used by Flex applications. Although the topic of ActionScript is far too complex to discuss comprehensively in one chapter, we have covered many of the basics you’ll need to get started writing ActionScript code, including where to place the code, basic syntax, common data types, how to write classes, the event model, error handling, working with XML, and reflection. 


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials