Get twitter ATOM search feed


/ Published in: ActionScript 3
Save to your folder(s)

There has to be a better way to do this rather than using regular expressions to remove the various namespaces within the ATOM feed. Also variables could be reused (currently used for teaching so helpful to explain process). Based on:

http://www.republicofcode.com/tutorials/flash/as3xml/
http://stackoverflow.com/questions/673412/how-can-i-remove-a-namespace-from-an-xml-document


Copy this code and paste it in your HTML
  1. var query:String = "greenwich";
  2. var myXML:XML;
  3. var myLoader:URLLoader = new URLLoader();
  4. myLoader.load(new URLRequest("http://search.twitter.com/search.atom?lang=en&q="+query));
  5. myLoader.addEventListener(Event.COMPLETE, processXML);
  6.  
  7. function processXML(e:Event) {
  8. myXML = new XML(e.target.data);
  9. myXML = removeNS(myXML);
  10. // then use myXML object as normal to access various parts of the feed
  11. for (var i:int = 0; i < myXML.entry.length(); i++){
  12. trace(myXML.entry.title[i]);
  13. }
  14. }
  15.  
  16.  
  17. function removeNS(anXML:XML){
  18. var xmlString = anXML.toXMLString();
  19. var xmlnsPattern = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
  20. var namespaceRemovedXML = xmlString.replace(xmlnsPattern, "");
  21.  
  22. var aaaPattern = new RegExp("aaa:lang[^\"]*\"[^\"]*\"", "gi");
  23. var aaaRemovedXML = namespaceRemovedXML.replace(aaaPattern, "");
  24.  
  25. var twitterPattern = new RegExp("twitter:", "gi");
  26. var twitterRemovedXML = aaaRemovedXML.replace(twitterPattern,"");
  27.  
  28. var openSearchPattern = new RegExp("openSearch:", "gi");
  29. var openSearchRemovedXML = twitterRemovedXML.replace(openSearchPattern,"");
  30.  
  31. var responseXML = new XML(openSearchRemovedXML);
  32. return responseXML;
  33. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.