Thursday, March 18, 2010

AS3 XMLList and default namespaces

There have been many days where I have relished in how easy Actionscript is to use and how advanced it is as a programming language. Today was not one of those days.

Ever tried to use the XMLList class with XML containing a default namespace? If you have, you already know what I am going to say. If you haven't, you need to know. It doesn't work right!

Nothing happens, literally. The XMLList will just be empty. You have 2 choices at this point. You can set the default namespace using default xml namespace = someNamespace, but that requires setting it back in the same function using default xml namespace = ''. If you don't, be prepared to see errors about accessing invalid registers. Why? Because you are changing the default namespace which has ramifications outside of your little block of code.

The only suitable option is to fully qualify your objects using the namespace. So instead of a accessing an element with myxml..myelement, you must now use myxml..myNamespace::myelement.

I probably could have spent more time on this blog post, but I spent most of my day dealing with the problem being described. Need more info or help? Let me know and I'd be glad to help.