Archive for June, 2009
XML Namespaces in AS3: XMLNamespaceProxy
Quick note about a neato class I just added to guttershark. I’ve been working with SOAP in AS3 lately. Man is it a pain in the butt. So I’m doing my best to make sure I don’t have to write this crap again.
One other quick note before I show you the namespace proxy. I’ve integrated a SoapService in with the ServiceManager. It’s fairly easy to setup now. You can read all about that in the docs.
On to the XMLNamespaceProxy. With soap, when you get a result back - there are a couple steps you need to go through. Usually looks something like this:
var result:XML=new XML(mySoapResultXML); var ns:Namespace=result.namespace(); var body:XML=result.ns::Body;
This sets you up with whatever is in the body of the soap result. Which is pretty simple. But now I have to snoop through the content of the body - which in my case (the service I’m using) has custom namespaces in the result. Here’s my initial code looked like:
var targetNodes:XML=body.ns::rawValues.ns::bud09Out; trace(targetNodes.ns::totalIncome); //give me a value im looking for
The service I’m hitting has namespaces in the responses, so in order to find the actual values needed I have to prefix everything with the qualifying namespace. Arg!
Here’s entire code snippet with xml namespace proxy integrated:
var result:XML=new XML(mySoapResultXML); var ns:Namespace=result.namespace(); var body:XML=result.ns::Body; var targetNodes:XML=body.ns::rawValues.ns::bud09Out; //the good stuff: var xnp:XMLNamespaceProxy=new XMLNamespaceProxy(targetNodes,ns); //now I can use the xml namespace proxy just like normal: trace(xnp.totalIncome); //or... trace(xnp.years.year[0].@category);
After you create an XMLNamespaceProxy object, every call get’s prefixed with the qualifying namespace and forwarded to the actual XML object. No more namespace prefix’s. Yay!
As you’re reading through the XML. The proxy will return instances of XMLNamespaceProxy’s (if the target is XML), or it will return a normal XMLList which you can iterate over.
And as usual, check the docs.
Enjoy!
4 comments