XML Parser for JavaScript - xml2array()

This page has been moved to a new location at XML Parser for JavaScript - xml2array() at OpenJS

xml2array() parses the given XML document and return the data in an associative array.

See Demo.

Useage

xjx.load("file.xml",function(xmlDoc) {
	var arr = xml2array(xmlDoc);
	// Do what you want with 'arr'
});

xjx Or Xml aJaX is a small library I wrote to load external XML files using the XMLHTTPRequest method. Based on the jx Ajax library. You don't have to use this library to parse the XML file - 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.

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 GPL license.

Related Scripts

Comments

Anonymous at 29 Jan, 2007 12:10
I get this error when trying this in my own code and using your RSS example in IE 6.029 on Win XP:

Line: 16
Char: 2
Error: 'arr.rss.channel' is null or nto an object
Code:0
URL:www.bin-co.com/javascript/scripts/xml_parser/xml2array_demp.php

Any ideas?
Reply to this.
Prasad Kancharla at 31 Jan, 2007 02:13

I got a problem when I call xml2array function for the second time ( I tried to convert the same 'xml content' for the second time , but it resulted in two different associative arrays .I checked in IE 6 , IE 7, mozilla 1.5, mozilla 2.x browsers) because of the
global variable " parent_count "
It's declared globally
as..

var parent_count = new Object;


So , I changed the above line as

var parent_count ;
//Process the xml data
function XML2ARRAY(xmlDoc)
{
parent_count = new Object;
var arr= xml2array(xmlDoc);
delete parent_count;
return arr;
}
Now, my problem got solved .....
And Thanx for the code ..
Reply to this.
at 31 Jan, 2007 10:30
Sorry about the bug - I have fixed it.

There is a problem with the code you have provided - it will not maintain the functionality of this function in large/complex xml files. I have made a small change to your code - you can see it at the updated page of xml2array()

By the way, I have moved this page to my new site - all the updates will be made there.
Reply to this.
Anonymous at 19 Feb, 2009 03:01
xml:
<mainbla>
<bla_1>asdf</bla_1>
<bla_2>asdf</bla_2>
<amount>3</amount>
</mainbla>

javascript:
var blaArray = xml2array(theXmlAbove);
alert( blaArray["mainbla"]["amount"] );

what I get is:
NOTHING!!
Reply to this.
Jimmy at 24 Feb, 2009 09:25
Seems to not work when an array element (i.e. array['key']) has a subarray with only one value as its child. Any ideas?
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




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.
Subscribe to Feed