Dynamically add element / attribute to a XML using E4X
Written for: Flex, ActionScript 3.0
To dynamically add element / attribute to a XML; assume that it already exists in the XML and using the .(dot) operator, pass a value to it.
Here’s an example. Consider the following XML:
var albumXML:XML = <tracks> <song tracknumber="1"> <title>Orgasm Addict</title> <artist>Buzzcocks</artist> </song> <song tracknumber="2"> <title>What Do I Get?</title> <artist>Buzzcocks</artist> </song> <song tracknumber="3"> <title>I Dont Mind</title> <artist>Buzzcocks</artist> </song> <song tracknumber="4"> <title>Love You More</title> <artist>Buzzcocks</artist> </song> </tracks>;
To add an additional element of <genre> to the first <song> node, I’ll use the following line of code.
albumXML.song[0].genre = 'Punk';
Similarly to add an attribute of duration to the first <song> node, I’ll use.
albumXML.song[0].@duration = '2:00';
trace(albumXML);
/*
<tracks>
<song tracknumber="1" duration="2:00">
<title>Orgasm Addict</title>
<artist>Buzzcocks</artist>
<genre>Punk</genre>
</song>
<song tracknumber="2">
<title>What Do I Get?</title>
<artist>Buzzcocks</artist>
</song>
<song tracknumber="3">
<title>I Dont Mind</title>
<artist>Buzzcocks</artist>
</song>
<song tracknumber="4">
<title>Love You More</title>
<artist>Buzzcocks</artist>
</song>
</tracks>
*/
Additionally, you can use the following methods of a XML object.
- appendChild()
- prependChild()
- insertChildAfter()
- insertChildBefore()
Video: Integration of Flex with Flash CS3
Found a video by Glenn Ruehle demonstrating integration of Flex with Flash CS3.
FlexBuilder: Can’t find NPSWF32.dll for Mozilla FireFox
Once in a while, while debugging a Flex application, I have been show this error by Flex Builder.
C:\Program Files\Mozilla Firefox\plugins\NPSWF32.dll
Flex Builder cannot locate the required debugger version of Flash Player. You might need to install the debugger version of Flash Player 9 or reinstall Flex Builder.
Do you want to try to debug with the current version?
This essentially means Mozilla Firefox (if you have set FireFox as the default browser for loading applications from Flex Builder) can’t find the debug version of Flash player. I still haven’t found out the exact reason why it occurs. But, I have found solutions over time which tackle it. (I have always tried each solution, one after another, as sometimes one solution works in one case while the others don’t.)
- Copy NPSWF32.dll file from C:\WINDOWS\system32\Macromed\Flash to C:\Program Files\Mozilla Firefox\plugins.
- Download and reinstall the latest Flash Debug player.
- Install Flex 2.0.1 patch for Adobe Flash CS3 Professional compatibility if you are using Flex Builder 2.
- And if all fails, (sigh) just reinstall Flex Builder.
Difference between Display Object and Display Object Container
In ActionScript 3.0, anything that appears on the application screen are types of display objects and display object containers are special types of display objects. Though both are visually represented in the application in the same way, the difference between them is that one can add child display objects to a display object container. That means, you can use a Sprite as a display object container and can add shapes, bitmaps, texts,… as child display objects.
How to split a pipe(|), comma(,), tab, space,… separated string in ActionScript 3.0
To split a pipe(|), comma(,), tab, space,…. separated string into multiple string values in ActionScript 3.0, you will have to use the split() method of String class. Below is short example where in a pipe(|) seperated string is broken up into multiple values and pushed into an array.
var cities:String = "Bombay|Los Angeles|Moscow|London|Rio de Janeiro|Sydney";
var citiesArray:Array = cities.split("|");
trace(citiesArray); //Result: Bombay,Los Angeles,Moscow,London,Rio de Janeiro,Sydney
Article: Introducing E4X
Found a great article on E4X by Kurt Cagle. Below is the link.
Commenting code in Flex Builder
Flex Builder provides useful keyboboard shortcuts to comment code. Below are the different ways you can do it.
ActionScript(AS) Code: To toggle
- Single Line Comment (// Lorem Ipsum)
Select single / multiple lines of code and hit Control+/ (Windows) or Command+/ (Mac OS).
- Multiple Lines Comment (/* Lorem Ipsum */)
Select single / multiple lines of code and hit Control+Shift+C (Windows) or Command+Shift+C (Mac OS).
MXML Code: To add
- XML Comment (<!– Lorem Ipsum –> )
Select single / multiple lines of code and hit Control+Shift+C (Windows) or Command+Shift+C (Mac OS).
- CDATA Block (<![CDATA[
Lorem Ipsum
]]> )
Select single / multiple lines of code and hit Control+Shift+D (Windows) or Command+Shift+D (Mac OS).
ActionScript reference guide for RIA development
Found a nice reference guide for ActionScript APIs.
From the site:
This ActionScript reference for rich Internet application development provides an alphabetical reference for all native ActionScript APIs for the Adobe technology platform runtimes: Adobe Flash Player and Adobe AIR—as well as the Adobe Flex framework APIs. Use this guide both as an API reference and a tool to learn about the ActionScript APIs available within the runtimes.
Thanks Adobe!

