Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.

Bin-Blog

XML Parser function for JavaScript - xml2array()

I have created a small function called 'xml2array()' that will take an XML document as the argument, parse it and return an associative array as the result. This array will contain the data of the XML file - which can be easily accessed - without resorting to writing codes with xml.childNode or node.nextSibiling or any other stuff like that. Take a look at the Demo page.

XML Parsing in JavaScript

One have to admit that parsing XML code in JavaScript is relatively easy. We already have a DOM parser in place and we can use the functions provided by it to parse the XML documents. The functions like getElementsByTagName() can be used to make parsing XML documents much easier.

Actionscript

Compare that with, say, ActionScript - they have no getElementsByTagName. This makes it much more difficult to parse XML documents. I was hoping that this function will make XML parsing easier for ActionScripters also. But before using this code in ActionScript, please note that I don't know ActionScript - I only know JavaScript - and since the two are very similar, I was hoping that my script will work in Flash too - since they need it much more than we do. But it don't - my script creates some errors in Flash - if any of you could get it working in flash, please let me know.

Code

Anyway, the script can still be used in JavaScript for parsing XML returns in Ajax scripts. So if you use XML as the response data format for your ajax program, be sure to get a copy of the code for xml2array.

0 Comments: