E4X XML Namespaces

Tuesday, November 6th, 2007

For XML that looks like the following response from Yahoo's weather service:

XML:
  1. <?xml version='1.0' encoding='UTF-8'?>
  2. http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*
  3. http://weather.yahoo.com/forecast/94089_f.html         Yahoo! Weather for Sunnyvale, CA
  4. en-us
  5. Tue, 06 Nov 2007 6:56 pm PST
  6. 60
  7.  
  8.  
  9.  
  10.  
  11.  
  12. <img alt="" />
  13.  
  14. 142
  15. 18
  16.  
  17. http://weather.yahoo.com/             http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif
  18. 37.39
  19. -122.03
  20.  
  21. http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/94089_f.html Tue, 06 Nov 2007 6:56 pm PST
  22.  
  23. &lt;![CDATA[
  24. <img src="http://l.yimg.com/us.yimg.com/i/us/we/52/33.gif" alt="" />
  25. <strong>Current Conditions:</strong>
  26. Fair, 55 F
  27.  
  28. <strong>Forecast:</strong>
  29. Tue - Mostly Clear. High: 67 Low: 49
  30. Wed - Partly Cloudy. High: 71 Low: 49
  31.  
  32. <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/* http://weather.yahoo.com/forecast/94089_f.html">Full Forecast at Yahoo! Weather</a>
  33. (provided by The Weather Channel)
  34. ]]&gt;
  35.  
  36.  
  37.  
  38. 94089_2007_11_06_18_56_PST

When you need to access the nodes with complex node names such as yweather:location think XML namespaces.
In the above example, look for the xmlns declaration in the rss node
- xmlns:yweather='http://xml.weather.yahoo.com/ns/rss/1.0'.
Creating a Namespace object using the declaration will allow us to access the nodes with complex names.
Creating a Namespace object is a pretty simple process:

Actionscript:
  1. var yweatherNS:Namespace = new Namespace( http://xml.weather.yahoo.com/ns/rss/1.0 );

Now we can access the node with the following code:

Actionscript:
  1. yahooWeaterXML.channel.yweatherNS::location
  2.  
  3. trace( yahooWeaterXML.channel.yweatherNS::location.@city ); // outputs 'Sunnyvale'
  4. trace( yahooWeaterXML.channel.yweatherNS::location.@region); // outputs 'CA'
  5. trace( yahooWeaterXML.channel.yweatherNS::location.@country ); // outputs 'US'

With the preceding example we need to know the namespace url ahead of time. E4X gives us the namespaceDeclarations() method that will return an array that contains the namespace declarations associated with the XML document

Using the namespaceDeclarations() method from the XML object:

Actionscript:
  1. var namespaces:Array = myXML.namespaceDeclarations();

Now we have an array of namespace declarations that we can use to dynamically declare Namespace objects and access our complex node names:

Actionscript:
  1. var yweatherNS:Namespace
  2. var geoNS:Namespace
  3. var nsLen:uint = nameSpaces.length;
  4. for(var i:uint = 0; i &lt;nsLen; i++)
  5. {
  6. var newNamespace:Namespace = new Namespace( nameSpaces[i] );
  7. if( String( nameSpaces[i].prefix ).toLowerCase() == "yweather" )
  8. {
  9. yweatherNS = newNamespace;
  10. }
  11. else
  12. {
  13. geoNS = newNamespace;
  14. }
  15. }

So we can access the geo and yweather nodes without any problems now.

More Flexible Code - Programming to an Interface

Wednesday, August 22nd, 2007

Keep your code flexible - program to interfaces. What does this mean? Well there are a few things that we need to discuss before we can really answer that question. For this discussion you'll need a decent understanding of Object Oriented programming…but, hey if you don't have that it still may make some sense anyhow and you might even get something that you don't understand explained for you as we go along.

First things first, Polymorphism - The ability for one class to stand in for another, kind of. By "kind of" I mean that by programming to an interface, a variable or property's value can be assigned to an object that conforms to a "formal interface", more on that in a second. This means that if you crate a property or variable that is typed to an interface - myPet:IAnimal; - (where IAnimal is the interface) that item can be any number of classes that follow the "interface" defined by IAnimal .

Example:

Actionscript:
  1. public var myPet:IAnimal = new Dog();
  2. public var yourPet:IAnimal = new Fish();
  3. public var hirPet:IAnimal = new Camel();
  4. public var herPet:IAnimal = new Giraffe ();

Each of the classes, Dog, Fish, Camel, Giraffe must "implement" the IAnimal interface.

Okay, so what is a formal interface you say?
First off, a formal interface is a definition, some might say a contract, but I think that is confusing, of what a class's publically accessible methods should be and what they should look like. ActionScript 3 provides a construct to create these interfaces that is very similar to classes. Basically you create the public methods, their parameters and their return types, but nothing that has to do with what happens inside the method.

Example:

Actionscript:
  1. package
  2. {
  3. public interface IAnimal
  4. {
  5. function create(p_name:String):void
  6. function feed(p_food:Object):void
  7. function sleep():void
  8. }
  9. }

Notice that there is nothing that tells you what you need to do in the methods, or anything about properties or private methods. That is determined by the needs of the class that implements the interface. This is a good point to take hold of and knock into your head:

A class has an implementation (what the class does ) and an interface (how the outside world interacts with it).

This is what allows your code to be flexible. If you program to interfaces, you can crate objects that are much more flexible and powerful by have the ability to "stand in" for any property or variable.

You can also create an "interface" through inheritance by using something called an "abstract" class. An abstract class isn't intended for instantiation, so it is up to you to manage that. But abstract classes do allow for a default implementation (what the class does) as well as allowing for the type of interface implementation mentioned above because sub-classes inherit the interface of their parent classes.

So, keep your code flexible, program to interfaces!

Continious Integration for Flash and Flex

Wednesday, June 27th, 2007

Lately I've been working with CruiseControl, a continuous integration tool, and unit testing (ASUnit and FlexUnit) for our ActionScript 2, ActionScript 3/Flex projects. With the help of a couple of some great posts from eyefodder and Peter Martin, I've finally come up with a nice little system that isn't too much of a headache to set up and is very easy to maintain once it is set up.

When I get some real time, I'll be sure to blog about the set up and give some walk-throughs on how to get everything up and running.

The basic 'gist' of continuous integration is:

To build your code as soon as it changes. This helps to identify problems with the source code as quickly as possible after the problem is introduced. By introducting unit tests into the build process, you add more integrity to the build as well as your code.

So, like I said as soon as I get some real time, I'll be sure to put together some set up information.

Dispatching a custom event

Saturday, March 31st, 2007

There are many common events for Flex components, such as the click or change events. These events are very usefull and great for many situations. Your application or custom component may require more from an event than is provided in these built-in events. Not a problem! You can dispatch any of the predefined events inherited by a component's superclass, as well as new, custom, events that you define within the component.

To dispatch a new event from your custom component, you must do the following:

  1. Create a subclass from the flash.events.Event class (or another event class) to create an event class that describes the event object. This step is optional.
    Actionscript:
    1. public class MyCustomEvent extends Event
    2. {
    3. static public var customEventName:String = "customEventName";        public var myEventData:*;
    4.  
    5. public function MyCustomEvent(type:String, p_eventData:*, bubbles:Boolean=false, cancelable:Boolean=false)
    6. {
    7. super(type, bubbles, cancelable);
    8.  
    9. myEventData = p_eventData;
    10. }
    11.  
    12. }

  2. Use the [Event] metadata tag to make the event public so that the MXML compiler recognizes it. This step is optional.
    Actionscript:
    1. [Event(name="customEventName", type="com.events.MyCustomEvent")]
    2. public class MyCustomEvent extends Event
    3. {    ...

  3. Dispatch the event using the dispatchEvent() method.
    Actionscript:
    1. dispatchEvent(new MyCustomEvent(MyCustomEvent.customEventName, eventData));

[Simple Custom Event Sample]

[Simple Custom Event Source]

Flex Doumentation:

Flex 2.0.2 hotfix released!

Thursday, March 22nd, 2007

Adobe has released a hotfix for the Flex SDK. Looks like there are quite a few things for the DataGrid.

Adobe - TechNote : Flex 2.0.1 SDK Hotfix 1

Flex 2.0.1 SDK hotfix 1 (build 159086) is a collection of bug fixes that have been completed since the Flex 2.0.1 release on January 5, 2007.I like this note: "Installation of this hotfix release is not recommended for customers who are not experiencing one of the described issues"

Apollo is on Labs - Houston we have blastoff!

Monday, March 19th, 2007

….yeah I know geeky. Apollo is now on Labs!

Apollo is a cross-OS runtime that allows developers to leverage their
existing web development skills (Flash, Flex, HTML, Ajax) to build and
deploy desktop RIA’s.

http://labs.adobe.com

FlexLib Launched

Wednesday, February 28th, 2007

Darron Shall announced on his blog that FlexLib is now launched.

FlexLib is a Google Code project that aims at being a concentrated effort by the Flex 2 community for creating high quality user interface components. FlexLib provides a standard component namespace, package structure, code and naming conventions, and common reusable base classes to unify the various efforts people have put forth on their own.

Also mentioned it FlexBox which Jun Heider brought to my attention yesterday. FlexBox is directory of Adobe Flex components available for developers.

Darron Shall's full post includes some additional Flex resources. Check it out and get Flexified!

LuminicBox goes AS3

Monday, February 19th, 2007

Missing the LuminicBox debugger in Flex 2? Mark Walters at digitalflipbook has translated the LuminicBox classes from AS2 to AS3. Now you can debug the way you're used to (although the debugger in Flex Builder is still pretty great).

Go grab the updated source:

[http://www.digitalflipbook.com/archives/2007/02/luminicboxlog_a.php]

and thanks Mark!

Find entries :

Want to subscribe?

  Subscribe in a reader or,
Subscribe via email:

About me

I'm a senior developer at RealEyes Media, Adobe Certified Instructor and Adobe Certified Professional. Here you'll get my ideas and experience Flex, Flash, ColdFusion and related technologies as well as some generally off the wall stuff.