Saturday, May 26, 2007

Using AS3.0 with E4X for Jabber IQ packet parsing? watch for the xmlns attrib in the query node

I'm working on a jabber based client, and recently I was attempting to implement a tiny widget, which would parse a jabber IQ packet and do some stuff with the data in it. Since it was more of a personal project, I thought it would be a good place to attempt some AS3.0 + E4X.

The first time I attempted to parse an attribute from a node, my trace returned me nothing. The sample XML provided in AS3.0 documentation was behaving as expected, but my IQ XML node wouldn't return me what I was looking for. And there no error message too. After a couple of unsuccessful attempts, I just closed the file and forgot about it.

Today I decided to give it another shot, and opened it up again. After a little examination of the IQ packet, I realized that the query node had an xmlns attribute in it. Duh! I wasn't handling the namespace :P

A quick search returned me this article by Darron. http://www.darronschall.com/weblog/archives/000223.cfm. So I went ahead and handled the xmlns as he mentioned in his article, and bingo! it works. Thanks Darron!

var req:XML = <iq type="get" id="01" to="xxx.xxxxx.com">
<query xmlns="xxx:iq:xxx">
<getXXX attrib="blah">
<authorized />
</getXXX>
</query>
</iq>;


// I had to add this
namespace ns= "xxx:iq:xxx"
use namespace ns;

trace(req.query.getXXX.@attrib);
So if you are parsing jabber iq packets, remember to handle the xmlns.

Yea, I know the AS3 XIFF library would most probably handle it all, but there were reasons why I cannot use the XIFF library for this purpose.
Post a Comment