Embracing E4X Namespaces in Flash


Every time I start working on a project where I end up using xml and flash, it usually seems pretty straight forward. I usually bounce between sephiroth and kirupa for getting my bearings. Initially it all ends up feeling fine, especially when working with a perfectly constructed test xml file.

It’s only when you attempt to parse an established XML type, such as RSS, where you feel the snags of dealing with XML. At first its not clear why things are a problem, you are targeting the correct child nodes, not spelling anything wrong. Thats when you start to trace out the xml and the namespace nightmare becomes apparent. What is the extra stuff being appended to the nodes? How do I target these things?

Maybe the first thing one tries is just appending the full namespace with the child nodes that you want to target, and maybe it will work, but most of the time it wont. Then comes the search engine APB, and information about various techniques some would have you enter full name space names, while others would have you set the current namespace. After the brush with name spaces you feel optimistic and are probably able to complete your project.

All is well. Projects are just flowing in, if XML comes up, it’s usually ones that you define and create yourself. But wait, another project calls for some xml parsing and manipulating. You now have to write valid XML with in a name space. Will this never end?

Well I’ve been going though Chem4Word and translating the key parts of that system to be used in flash. While interpreting c# and turning it to as3 sounds like a pedestrian ordeal, one of the challenges is to convert LINQ ( c# method for dealing with XML ) to E4X notation, which lacks many of the features LINQ has. So I’d like to share some techniques that helped me work though the translation.

1) Set default namespace before any block of code dealing with XML. (It’s useful when dealing with 2 different namespaces in one XML document)


var ns:Namespace = new Namespace("http://lol.namespaces.can/suck");

default xml namespace = ns; // feel free to set this as often as possible in a project
//default xml namespace = new Namespace(); // this resets/blanks the namespace
var test:XML =  <test id="hey"/>;

var idStr:String = "id";

trace( test.toXMLString() );
//traces: <test id="hey" xmlns="http://lol.namespaces.can/suck"/>

test["@"+idStr] = "omfg_I_h8_j00";

trace( test.toXMLString() );
//traces: <test id="omfg_I_h8_j00" xmlns="http://lol.namespaces.can/suck"/>

Taking a look at the text:XML var and the trace outputs, you notice that AS3 automatically inserted in the xmlns element into the xml. This happened because we created new xml after setting the default xml namespace. If you comment out the default namespace line OR uncomment the line that sets the namespace to new Namespace() then the xml will not have any xmlns inserted automatically.

2) LocalName() vs Name(). Taking the rest of the code from the first example, take a look at this:

trace( test.name() ); // traces: http://lol.namespaces.can/suck::test
trace( test.localName() ); //test

So you are already in the midst of working in namespace code, and you are trying to compare some node names or something. Things just aren’t working out and you trace out to see what the problems are. Thats when the name() also outputting the namespace becomes apparent. Instead of splitting on ‘::’ just switch to localName() and you’ll have the node name by itself. I’d imagine this one is more obvious, but I didn’t immediately know it existed when working with e4x.

3) Dynamic attributes references need to be written as such:

xmlnode['@' + attributeString]

It’s also in the first code example, but I needed dynamically target various elements in the xml. I found this Stack Overflow topic and it lead me down a wrong path. I guess it was foolish of me to just trust and assume they were correct, but I did and it gave me problems. I added my explanation to the thread, so if anyone reading this has a Stack Overflow account that can upvote (and downvote) I’d appreciate being the 2nd response in the solutions.

I hope that some one stumbles upon this when having to deal with as3 xml namespace projects, because many of the search results dealing with as3 xml namespaces seem to ask how to strip them out which ultimately is the wrong direction to move in, even if you hate working with them ‘oh so much.’