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.

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

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 .

License

This script is released in the BSD license.

Related Scripts

Comments

Extory at 21 Mar, 2007 09:24
when I was count the length of array after transported.
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?

Reply to this.
Binny V A at 22 Mar, 2007 05:53
Because Object represents associative arrays and Array is for Lists. Take a look at these links...
JavaScript "Associative Arrays" Considered Harmful
Javascript Associative Arrays considered harmful
Reply to this.
Luklatlug at 10 May, 2007 04:19
I haven't looked at the code yet, but as far as I know, ActionScript2 (and lesser) does not support regular expressions. AS3 has a native Regular Expressions support, though, but since I don't use AS3 compilers yet, I cannot be of any help to you.
Cheers
Reply to this.
Amjad at 20 Jun, 2007 02:30
hey Binny ...u r jus 2 gud.. was lukin for such code...
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!!!![:)]
Reply to this.
Senning at 17 Mar, 2008 07:19
Thanks Binny V A! This works like a charm... except when responseXML returns a root with only one child. For example, it'll work as expected with


<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?
Reply to this.
Alexian at 09 Apr, 2008 05:40
Yes, I have.
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;
}
}
Reply to this.
Anonymous at 13 Jul, 2007 03:01
Why is it GPL-licensend, and not LGPL... very fine........ ;(
Reply to this.
Binny V A at 13 Jul, 2007 06:16
You got me there - I usually use BSD license for all my code. I have no idea why I used GPL for this. Just give me some time - I will change it to BSD.
Reply to this.
Anonymous at 19 Oct, 2007 12:54
it's responseXML not responseXml
Reply to this.
Jaakko at 12 Nov, 2007 04:00
Thanks for nice script, but "attribute_inside = 1;" config does not work if there is more than one attribute in element. It totally screws up the element.
Reply to this.
Anonymous at 14 Nov, 2007 04:30
just testing
Reply to this.
Jaycode at 04 Dec, 2007 08:28
I just want to say heaps thanks for the code you developed. It was one of the best! ;)

All the best for you, BinnyVa
Reply to this.
Chat at 17 Feb, 2008 06:45
config does not work if there is more than one attribute in element
Reply to this.
Anonymous at 20 Feb, 2008 01:28
XmlHttp.onreadystatechange = function() {
if(XmlHttp.readyState == 4 && XmlHttp.status == 200) {
var arr = xml2array(XmlHttp.responseXml);
// Do what you want with 'arr'
}
});
Reply to this.
trosen at 22 Apr, 2008 11:14
attribute_inside = 0 ?

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?
Reply to this.
Binny V A at 30 Apr, 2008 11:08
This really comes in useful if the XML in question dont have any attributes - or you don't care about the attributes.
Reply to this.
Comment


Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.