Return to Snippet

Revision: 23689
at February 11, 2010 10:45 by eddequincey


Updated Code
var query:String = "greenwich";
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("http://search.twitter.com/search.atom?lang=en&q="+query));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event) {
	myXML = new XML(e.target.data);
	myXML = removeNS(myXML);
        // then use myXML object as normal to access various parts of the feed
	for (var i:int = 0; i < myXML.entry.length(); i++){
		trace(myXML.entry.title[i]);
	}
}


function removeNS(anXML:XML){
	var xmlString = anXML.toXMLString();
	var xmlnsPattern = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
	var namespaceRemovedXML = xmlString.replace(xmlnsPattern, "");

	var aaaPattern = new RegExp("aaa:lang[^\"]*\"[^\"]*\"", "gi");
	var aaaRemovedXML = namespaceRemovedXML.replace(aaaPattern, "");
	
	var twitterPattern = new RegExp("twitter:", "gi");
	var twitterRemovedXML = aaaRemovedXML.replace(twitterPattern,"");
	
	var openSearchPattern = new RegExp("openSearch:", "gi");
	var openSearchRemovedXML = twitterRemovedXML.replace(openSearchPattern,"");

	var responseXML = new XML(openSearchRemovedXML);
	return responseXML;	
}

Revision: 23688
at February 11, 2010 10:31 by eddequincey


Updated Code
var query:String = "greenwich";
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("http://search.twitter.com/search.atom?lang=en&q="+query));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event) {
	myXML = new XML(e.target.data);
	myXML = removeNS(myXML);
        // then use myXML object as normal to access various parts of the feed
	trace(myXML.entry.title);
}


function removeNS(anXML:XML){
	var xmlString = anXML.toXMLString();
	var xmlnsPattern = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
	var namespaceRemovedXML = xmlString.replace(xmlnsPattern, "");

	var aaaPattern = new RegExp("aaa:lang[^\"]*\"[^\"]*\"", "gi");
	var aaaRemovedXML = namespaceRemovedXML.replace(aaaPattern, "");
	
	var twitterPattern = new RegExp("twitter:", "gi");
	var twitterRemovedXML = aaaRemovedXML.replace(twitterPattern,"");
	
	var openSearchPattern = new RegExp("openSearch:", "gi");
	var openSearchRemovedXML = twitterRemovedXML.replace(openSearchPattern,"");

	var responseXML = new XML(openSearchRemovedXML);
	return responseXML;	
}

Revision: 23687
at February 11, 2010 10:26 by eddequincey


Updated Code
var query:String = "greenwich";
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("http://search.twitter.com/search.atom?lang=en&q="+query));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event) {
	myXML = new XML(e.target.data);
	myXML = removeNS(myXML);
	trace(myXML.entry.title);
}


function removeNS(anXML:XML){
	var xmlString = anXML.toXMLString();
	var xmlnsPattern = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
	var namespaceRemovedXML = xmlString.replace(xmlnsPattern, "");

	var aaaPattern = new RegExp("aaa:lang[^\"]*\"[^\"]*\"", "gi");
	var aaaRemovedXML = namespaceRemovedXML.replace(aaaPattern, "");
	
	var twitterPattern = new RegExp("twitter:", "gi");
	var twitterRemovedXML = aaaRemovedXML.replace(twitterPattern,"");
	
	var openSearchPattern = new RegExp("openSearch:", "gi");
	var openSearchRemovedXML = twitterRemovedXML.replace(openSearchPattern,"");

	var responseXML = new XML(openSearchRemovedXML);
	return responseXML;	
}

Revision: 23686
at February 11, 2010 09:49 by eddequincey


Initial Code
var query:String = "greenwich";
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("http://search.twitter.com/search.atom?lang=en&q="+query));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event) {
	myXML = new XML(e.target.data);
	myXML = removeNS(myXML);
	trace(myXML.entry.title);
}


function removeNS(anXML:XML){
	var xmlString = anXML.toXMLString();
	var xmlnsPattern = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
	var namespaceRemovedXML = xmlString.replace(xmlnsPattern, "");

	var aaaPattern = new RegExp("aaa:lang[^\"]*\"[^\"]*\"", "gi");
	var	aaaRemovedXML = namespaceRemovedXML.replace(aaaPattern, "");
	
	var twitterPattern = new RegExp("twitter:", "gi");
	var twitterRemovedXML = aaaRemovedXML.replace(twitterPattern,"");
	
	var openSearchPattern = new RegExp("openSearch:", "gi");
	var openSearchRemovedXML = twitterRemovedXML.replace(openSearchPattern,"");

	var responseXML = new XML(openSearchRemovedXML);
	return responseXML;	
}

Initial URL

                                

Initial Description
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

Initial Title
Get twitter ATOM search feed

Initial Tags
api, twitter

Initial Language
ActionScript 3