return undefined;


Dealing with (default) namespaces in Flex 2/AS3

Posted in Flash, Flex by Ben Clinkinbeard on the July 14th, 2006

Since the release of Beta 2, I have been playing with Flex 2 and AS3 whenever I've had spare moments. Most of my tinkering has been focused around calling SOAP web services as that is what is most applicable to my current work situation. A problem I kept running into was that the data returned contains default namespaces (i.e. no prefix, just xmlns="yaddayadda"), and at present there is a dearth of information on working with them. Long story short, you must specify which namespace you want to use for each and every operation, unless you use this approach:

Actionscript:
  1. namespace foo = "http://site.com/foo";
  2. use namespace foo;

I really didn't like the idea of having those directives scattered about in my app, in every file that needed to work with my XML data. Today, guided by an initial tip from Brian, I've got it working. This is more or less a direct implementation of the examples shown in the docs applied to XML, rather than methods.

What follows is my initial take on how to approach the use of several namespaces in a Flex 2 application that uses the Cairngorm framework. I am a Flex and Cairngorm noob, so if someone has suggestions on a better structure, I am all ears. The approach is based around creating package-level namespace files, that you can then import into wherever you need them.

Here is a sample namespace file:

Actionscript:
  1. package com.fmr.nstest.business.namespaces
  2. {
  3.     public namespace CLIENT_MEASURES_NAMESPACE = "http://site.com/BackOffice/CMeasures";
  4. }

I decided the most logical place to put these files was in a 'namespaces' package inside the 'business' package, which is where my Delegates and Services.mxml file reside. I am defining them in all caps since they are more or less constants. You can only define one namespace per file, however, because there can only be one externally accessible definition per file in AS3. Using these files is pretty straightforward as well. Simply import the file (it will not show up in the auto-complete list in Flex Builder) and then insert your use namespace directive where appropriate:

Actionscript:
  1. import com.fmr.nstest.business.namespaces.CLIENT_MEASURES_NAMESPACE;
  2. ...
  3. use namespace CLIENT_MEASURES_NAMESPACE;

This approach is flexible in that if you put the use namespace directive before your actual class declaration starts, it can essentially be used as the default namespace for all the methods of your class. On the other hand, you could also import everything (*) from your namespaces package and place different use namespace directives in your methods so that they can address different sets of data.

3 Responses to 'Dealing with (default) namespaces in Flex 2/AS3'

Subscribe to comments with RSS or TrackBack to 'Dealing with (default) namespaces in Flex 2/AS3'.

  1. Phil Chung said,

    on March 23rd, 2007 at 9:02 pm

    Actually, this structure is good enough for Adobe as they use it in their framework :)

    Within the framework, there are some methods/props namespaced to mx_internal. This is because some api bits need to be accessible from other classes, so private and protected won't work, and they aren't meant to be public. So they've done the same thing...you'll find a file at mx/core/mx_internal.as which defines the namespace for mx_internal and then call:

    use namespace mx_internal

    whenever things in that namespace need to be accessed.

  2. Rahul said,

    on July 3rd, 2007 at 1:12 am

    Hi there,
    I am also a flex buff.. recently in one of my app, I used a webservice, which returned data in xml format. Well thats how u always get it.
    But it had so many namespaces in its root tag. I managed to remove all of them except one which is default i.e. no prefix (xmlns:"blahblah"). I used removeNamespace() method on the xml object.

    Will you suggest me one method to get rid of it?
    As I am not able to break my xml code into pieces because of this namespace..
    Thanks in advance

  3. Ben said,

    on July 3rd, 2007 at 8:37 pm

    Hi Rahul, I tried getting rid of it also when I first started with Flex but never really followed through as the method described in the post works well enough for me. Only thing I can think of would be to maybe convert the XML to a String, strip out the namespace and then parse it back to XML.

Leave a Reply