E4X XML Namespaces

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

< ?xml version='1.0' encoding='UTF-8'?>

http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*

http://weather.yahoo.com/forecast/94089_f.html         Yahoo! Weather for Sunnyvale, CA
en-us
Tue, 06 Nov 2007 6:56 pm PST
60

<img alt="" />

142
18

http://weather.yahoo.com/             http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif
37.39
-122.03

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

&lt; ![CDATA[
<img src="http://l.yimg.com/us.yimg.com/i/us/we/52/33.gif" alt="" />
<strong>Current Conditions:</strong>
Fair, 55 F

<strong>Forecast:</strong>
Tue - Mostly Clear. High: 67 Low: 49
Wed - Partly Cloudy. High: 71 Low: 49

<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>
(provided by The Weather Channel)
]]&gt;

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:

[as]
var yweatherNS:Namespace = new Namespace( http://xml.weather.yahoo.com/ns/rss/1.0 );
[/as]

Now we can access the node with the following code:

[as]
yahooWeaterXML.channel.yweatherNS::location

trace( yahooWeaterXML.channel.yweatherNS::location.@city ); // outputs ‘Sunnyvale’
trace( yahooWeaterXML.channel.yweatherNS::location.@region); // outputs ‘CA’
trace( yahooWeaterXML.channel.yweatherNS::location.@country ); // outputs ‘US’
[/as]

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:

[as]
var namespaces:Array = myXML.namespaceDeclarations();
[/as]

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

[as]
var yweatherNS:Namespace
var geoNS:Namespace
var nsLen:uint = nameSpaces.length;
for(var i:uint = 0; i < nsLen; i++)
{
var newNamespace:Namespace = new Namespace( nameSpaces[i] );
if( String( nameSpaces[i].prefix ).toLowerCase() == “yweather” )
{
yweatherNS = newNamespace;
}
else
{
geoNS = newNamespace;
}
}
[/as]

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

About John Crosby

A former professional foodie turned keyboard-jockey in favor of a keyboard, cushy chair and cooler working conditions. I now work at Realeyes Media. I am a Partner and Senior Developer and the office Kuroko: In kabuki, the kuroko serve many of the same purposes as running crew. They move scenery and props on stage, aiding in scene changes and costume changes. They will also often play the role of animals, will-o-the-wisps, or other roles which are played not by an actor in full costume, but by holding a prop. Kuroko wear all black, head to toe, in order to imply that they are invisible and not part of the action onstage.
This entry was posted in news and tagged , , , , , . Bookmark the permalink.

3 Responses to E4X XML Namespaces

  1. Parag says:

    Hi John,

    very good explanination of using namespace in xml. I was looking for same thing and was really helpful for me.

    Good work. Keep it up.

    Thank you,
    Parag

  2. Sunil says:

    Good article on using namespaces. Only issue with the last example is that you need to know possible prefixes used in the XML document. Isn’t that more problematic than knowing the URIs? Once you know the URI, any prefix can be used in your own code.

  3. jccrosby says:

    Sunil – I agree, that would make it much more simple. But, when using others services – (1) I try to keep things very explicit for “self-documentation”, (2) at some point you’ll have to either know or assign the namespace to some variable, so why not keep it the same and (3) as far as the example goes…I was learning as I was going. Any thoughts on how it might be done more elegantly would be appreciated.
    - John

Leave a Reply