AS3 – XML Namespace

May 7th, 2009 at 10:02 am by Rick

There are two ways to define namespaces :

1. The namespace is defined in the tag with theĀ attribute named “xmlns”.

XML :

1
2
3
4
5
6
<enclosure xmlns="http://www.solitude.dk/syndication/enclosures/">
 <title>Firenze</title>
 <link length="1" type="image/png">
  <url>http://kuler-api.adobe.com/kuler/themeImages/theme_24198.png</url>
 </link>
</enclosure>

2. The namespace is defined in a parent node with the attribute xmlns, and defines the prefix of the namespace that can be used later. In this example, there are three namespaces defined with the prefixes “xs”, “kuler” and “rss”.

XML :

1
2
3
4
5
6
7
8
9
<rss version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:kuler="http://kuler.adobe.com/kuler/API/rss/" xmlns:rss="http://blogs.law.harvard.edu/tech/rss">
 <channel>
  <link>http://kuler.adobe.com/</link>
  [...]
  <item>
   <kuler:themeItem>
    <kuler:themeID>24198</kuler:themeID>
    <kuler:themeTitle>Firenze</kuler:themeTitle>
    [...]

In Actionscript, to access a tag that is contained in a namespace, you create an instance of the Namespace class and then you access the node as you would normally do using the dot syntax, but you use the prefix of the namespace followed by “::” to indicate that it’s in that namespace :

AS3 :

1
2
var kuler:Namespace = new Namespace("http://kuler.adobe.com/kuler/API/rss/");
var title:String = xml_data.item.kuler::themeItem.kuler::themeTitle;

Comments are closed.