My buddy pointed out another approach that I show here — it’s easier and doesn’t actually remove the namespace.
XML namespaces can be a real pain in the a$$ in AS3, so I often just rip them out. I mean think about it…when you get some XML payload back in a web service response, do you really care or need the namespace? Nah….just rip that bad boy out and starting using your XML baby.
Here’s my quick and dirty AS3 XML Namespace Ripper — for the sake of argument and a quick example, just assume this is in a Responder object like a Caringorm business delegate, but it should probably exist in a static XML util:
public function result(response:Object):void
{
var xmlString:String;
var xmlnsPattern:RegExp;
var namespaceRemovedXML:String;
var responseXML:XML;
// get the XML payload
if(ResultEvent(response).result is XMLList)
{
response = ResultEvent(response).result as XMLList;
}
else
{
response = ResultEvent(response).result as XML;
}
// convert it to a string
xmlString = response.toXMLString();
// define the regex pattern to remove the namespaces from the string
xmlnsPattern = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
// remove the namespaces from the string representation of the XML
namespaceRemovedXML = xmlString.replace(xmlnsPattern, "");
// set the string rep. of the XML back to real XML
responseXML = new XML(namespaceRemovedXML);
// do something with the XML
}
Thanks for the quick refresher on regex Johnny!
It blows up if there is more than one namespace however, because the children elements that use typed namespaces are then not bound. I changed regex to
new RegExp(“xmlns=[^\"]*\”[^\"]*\”", “gi”);
to just remove the default namespace and it worked great. I can still access the bound children using the :: syntax. Anyways, thanks for the example.
This is a great post. I’ve been working on this all day, and this is the only solution I’ve found that works! Thanks!!!
Just curious about the reasoning…
Why don’t you just use the namespace that is sent back? If you are calling the web service you’ll know what namespace is being returned.
namespace nsToUse = “http://api.website.com/api/”;
use namespace nsToUse;
var xlc:XMLListCollection = new XMLListCollection(xml.YourNodeHere);
Much better for performance.
@Joe:
Because if you try to use the XMLListCollection as a dataprovider for a DataGrid it will not work without using a labelFunction.
This is a great post. I’ve been working on this all day, and this is the only solution I’ve found that works! Thanks!!!
what you should do is use a namespace proxy object – everything you attempt to access first goes through the proxy object, forwards the call the XML object, but automatically prefixes it with the namespace.
like this:
http://codeendeavor.com/gsdocs/net/guttershark/util/XMLNamespaceProxy.html
You should not remove the namespace!
A very easy way to get rid of them, is:
Replace the :: by _ !
<media::thumbnail .. becomes <media_thumbnail …
No namespace, but no conflict in your XML!
That is the solution!
Nop:
Use:
buffer = buffer.replace(new RegExp(‘<*:','gi'),'_');
Sorry