Using e4x with XML Namespaces for Twitter search

I was working on a component for a client that uses to find all relevant tweets based on a query string recently and I noticed there’s a lot of other samples out there that use some hefty XML parsing code to sift the data rather than make life easy with e4x. The Twitter search API uses XML namespaces in its results, and it seemed that many other posts out on the intertoobs (can I coin this term?) were implying that it caused problems within AS3. Not true. Since I didn’t find a lot on the Twitter search topic specifically, a new blog post was born. The following is a quick and easy sample of how to use e4x in your XML results by using the given XML namespaces.

For this client’s component, I didn’t need to authenticate users, but rather, search for all public streams via a query string. If you take a look at the Twitter search API docs you can see the process pretty straightforward, it’s just using URL query strings to retrieve data . Generally, you can search for tweets:

  • containing a word
  • from a user
  • to a user
  • referencing a user
  • containing a hashtag
  • or combining any of these options together

There are also fancy things you can do to limit or filter results, but you can check them out in the API docs. When you receive a result from your search, you’ll see how Twitter makes use of namespaces in the XML. The first massive wad of nodes in the XML are metadata, but what we’re looking for in this example are the entry nodes that contain actual Tweets. If you dissect one closer, you’ll see the namespaces for google, openSearch, and “” (an empty string).

<entry xmlns:google="" xmlns:openSearch="" xmlns="" xmlns:twitter="">
  <link type="text/html" rel="alternate" href=""/>
  <title>RT @adobegroups: Community Translation is now live on #AdobeTV! Learn how you can become a translator, or watch translated episodes</title>
  <content type="html">RT &lt;a href=""&gt;@adobegroups&lt;/a&gt;: Community Translation is now live on &lt;a href="" onclick="pageTracker._setCustomVar(2, 'result_type', 'recent', 3);pageTracker._trackPageview('/intra/hashtag/#AdobeTV');"&gt;#AdobeTV&lt;/a&gt;! Learn how you can become a translator, or watch translated episodes &lt;a href=""&gt;http://&lt;b&gt;adobe&lt;/b&gt;.ly/i0PFOv&lt;/a&gt;</content>
  <link type="image/png" rel="image" href=""/>
  <twitter:source>&lt;a href="" rel="nofollow"&gt;TweetDeck&lt;/a&gt;</twitter:source>
    <name>adobegroups (Adobe Groups)</name>

In the entry node, the xmlns=”” with no prefix points to the Atom syndication format specification. Atom is just another syndication format used for web feeds like RSS. Why Atom and not RSS you ask? Who knows? Some say Atom is better than RSS for this reason or that, some say we’re all made of tiny Atoms (not the same atoms, but still fascinating). Who’s to say really? I’m sure the developers at Twitter would say, but let’s not go off on a tangent here.

The XML namespace for the Atom specification in the Twitter search results does not use a prefix like xmlns:google for example. Take a look.

<entry xmlns:google="" xmlns:openSearch="" xmlns="" xmlns:twitter="">

So into the Flex code, the first thing you’ll do is make use of your friend, the Namespace class, a.k.a the Namespace class to easily reference the data you’re looking for with e4x.

You’d typically make use of an HTTPService component for the search and set a result handler method to parse the XML returned from the search like so:

// script block
protected function handleResult(event:ResultEvent):void
	var x:XML = event.result as XML; // set the XML result
	var ns:Namespace = x.namespace(""); //  set the ns to an empty string like the result xml
	trace(ns); // outputs set in the result xml from Twitter

	tweetsAC = new XMLList(x.ns::entry);

	for(var i:int = 0; i< tweetsAC.length(); i++){
		txtArea.htmlText += tweetsAC[i].ns::content.text() + "\n"; // notice use of the ns with double colon ::
		txtArea.htmlText += tweetsAC[i].ns::author.ns::name.text() + "\n\n";


		<s:HTTPService id="twitterSearchService"
					   fault="handleFault(event)" />

By making use of your namespace and referencing XML nodes with the double colon (::) you can do away with the infinite looping through nodes of old, and harness the power of e4x yet again. First, you set an actual Namespace variable in the code:

var ns:Namespace = x.namespace("");

Which enables you to reference any node as shown in the following:

trace(tweetsAC[i].ns::author.ns::name.text()); // traces the author's name from the <entry /> XML node

Now instead of looping for all of eternity to drill down into the XML, you can make use of e4x and pull the Tweets with few lines of code. The full source code is available for Flex 4 TwitterSearch.fxp.



  1. Pingback: Tweets that mention Using e4x with XML Namespaces for Twitter search « David Flatley --

  2. sh_V

    Great ..

    I was really looking something like this…….


Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.