XML Parser for JavaScript - xml2array()
xml2array() parses the given XML document and return the data in an associative array.
See Demo.
Useage
XmlHttp.onreadystatechange = function() {
if(XmlHttp.readyState == 4 && XmlHttp.status == 200) {
var arr = xml2array(XmlHttp.responseXml);
// Do what you want with 'arr'
}
});
Just load the XML file using the method you are most comfortable with and then call the xml2array function with the XML object as the argument.
Code
The code is given in the xml2array.js file. It is a bit ugly - but it gets the job done. This file also includes the code for the xjx library and the dump function(Javascript equivalent of PHP's print_r function).
The code is somewhat big(100 lines of code) so I am not putting it in this page - just have a look at the js file for the code. You need just the xml2array() function and the two global variables at top - there are two other functions there - just ignore them.
Problems
There are some problems with the script - but this just the first release so it is to be expected.
I have only tested the script in the following browsers. Please let me know how the script worked in other browser's.
- Firefox 1.5 in Linux
- Firefox 1.5 in Windows
- IE 6 in Windows
- Mozilla in Linux
Another problem is that the scripts runs into trouble when such details like XML version(<?xml version="1.0"?>) or stylesheet information(<?xml-stylesheet type="text/xsl" href="softwares.xsl" ?>) or some other extra infomation like <!DOCTYPE programs SYSTEM "softwares.dtd"> is there in the XML file. I am hopping this will not be much of a problem as this script is aimed for use with Ajax responses in XML format - so we can safely assume that there will not be any 'stylesheet' information in it ;-). If you find any solution for this problem, please let me know - my email is moc.liamg@avynnib
Actionscript
I was hoping that I could get this script to work in Actionscript. I don't 'know' actionscript per se - I know javascript and assume that I could code in Actionscript as they have very similar syntax. Unfortunately, the script fails in Actionscript - some problem with the whitespace Regular Expression. If any of you know actionscript, please take a look at the script and if you could get it working in Flash, drop me a line at moc.liamg@avynnib.
License
This script is released in the BSD license.

Comments
javascript response "undefinded".
//xml2array code
arr = new Object;
and I try to modified to arr = new Array();
is OK.
I want to ask why not define to Object not is array in the codes?
JavaScript "Associative Arrays" Considered Harmful
Javascript Associative Arrays considered harmful
Cheers
well i can run d code without any errors...
how ever if i try to alert(arr)...it says object...
and in some places it says undefined...
plz tell me how do i get the parsed content thts there in the arr!!!
plz reply asap!!!![:)]
<root>
<child>bullying jerk</child>
<child>all-round awesome</child>
</root>
but not as much with
<root>
<child>slightly spoiled, but pretty rad</child>
</root>
Has anyone else had this problem?
I added additional condition for parent compare.
if(xmlDoc.nodeName && xmlDoc.nodeName.charAt(0) != "#") {
if ( (xmlDoc.childNodes.length > 1)
|| ( (xmlDoc.childNodes.length == 1) && (xmlDoc.childNodes[0].childNodes.length > 0) ) ) { //If its a parent
arr = new Object;
parent = xmlDoc.nodeName;
}
}
All the best for you, BinnyVa
if(XmlHttp.readyState == 4 && XmlHttp.status == 200) {
var arr = xml2array(XmlHttp.responseXml);
// Do what you want with 'arr'
}
});
Default behaviour is to place attributes outside, ie. in parents tag and implies that attributes can not be used in the root-tag
In what kind of situations is there an advantage to have attributes placed outside?
a, strong, em, b, i, code, pre, pandbrallowed. Other tags will be shown as code(< will become <). Urls, Line breaks will be auto-formated.