I should have added this code to my previous post on clearing namespaces out of XML. Instead of editing that post, which was plenty long, I am opting to just post the class and usage example here. A link to the sample project, and its source code, located at the end of the post.

Utility Class

public class NamespaceRemover
{
public static const NAME:String = "NamespaceRemover";

public function NamespaceRemover(){}

public static function removeNamespaces( xml:XML ):XML
{
var cleanXML:XML;
var xmlSource:String;

xmlSource = xml.toXMLString();
xmlSource = xmlSource.replace(/<[^!?]?[^>]+?>/g, cleanXMLNamespaces);

cleanXML = XML(xmlSource)

return cleanXML;
}

private static function cleanXMLNamespaces(...rest):String
{
rest[0] = rest[0].replace(/xmlns[^"]+\"[^"]+\"/g, "");
var attrs:Array = rest[0].match(/\"[^"]*\"/g);
rest[0] = rest[0].replace(/\"[^"]*\"/g, "%attribute value%");
rest[0] = rest[0].replace(/(<\/?|\s)\w+\:/g, "$1");
while (rest[0].indexOf("%attribute value%") > 0)
{
rest[0] = rest[0].replace("%attribute value%", attrs.shift());
}
return rest[0];
}

}

I decided to make both of the methods in this class static. This way it can be used as a class, without instantiation (see Usage sample below). I didn't see any reason to waste the extra typing needed to create an instance of the class and then invoke the method to remove namespaces.

Usage
The following code snippet is from a recent PureMVC-Flex application on which I am working (slightly altered for readability, etc.).

import NamespaceRemover;
private function reportHistoryReceived( evt:ResultEvent ):void
{
var dsReport:XMLList;
var reportHistoryXML:XMLList;
var xml:XML;
var item:XML;
var reportHistory:ReportHistory;
var acReportHistory:ArrayCollection = new ArrayCollection();

xml = NamespaceRemover.removeNamespaces( evt.result[0] as XML );
dsReport = xml..*::DsReport;
reportHistoryXML = dsReport.children();

for each( item in reportHistoryXML )
{
// build up an ArrayCollection of ReportHistory objects
reportHistory = new ReportHistory();
reportHistory.Field0 = item.Field0;
reportHistory.Field1 = item.Field1;
// add to AC
acReportHistory.addItem( reportHistory );
}

// send a notification, passing the AC along for the ride
sendNotification( ApplicationFacade.REPORT_HISTORY_RECEIVED, acReportHistory );
}

I've created and uploaded a sample application that utilizes a free stock quote web service. The sample has source code enabled (right-click on the app's web page and select View Source). It's available at: http://www.baddogsconsulting.com/projects/namespaceremover/.

Please note that I am using a free web service and, sometimes, it goes offline/down. The Web Service I'm using is:
http://www.webservicex.net/stockquote.asmx?wsdl

You can download the code, change the line with this WSDL location to any other one you want, and recompile the application if you so desire.

Also note that the Get Quote button will not be enabled unless the WSDL loads. If it doesn't enable, the Web Service must still be unavailable (all web services at this site have been taking a while to load this morning, Thursday, May 7). Of course, the purpose was less of "using it" and more to show the source code for an example application using the NnamespaceRemove utility.

Comments

Craig Kaminsky
You may, if you wish, download the source code from:
http://s3.amazonaws.com/oss-projects/namespace.remover.flex.app.zip
Mayur Kaul
Great function...helped me a lot...
Stephane Schittly
Thanks!!! Really helpful ;)