xml2Json = function(xml) {
	var obj = {};

	if (xml.nodeType == 1) { // element
		// do attributes
		if (xml.attributes.length > 0) {
			obj['@attributes'] = {};
			for ( var j = 0; j < xml.attributes.length; j++) {
				obj['@attributes'][xml.attributes[j].nodeName] = xml.attributes[j].nodeValue;
			}
		}

	} else if (xml.nodeType == 3) { // text
		obj = xml.nodeValue;
	}

	// do children
	if (xml.hasChildNodes()) {
		for ( var i = 0; i < xml.childNodes.length; i++) {
			if (typeof (obj[xml.childNodes[i].nodeName]) == 'undefined') {
				obj[xml.childNodes[i].nodeName] = xml2Json(xml.childNodes[i]);
			} else {
				if (typeof (obj[xml.childNodes[i].nodeName].length) == 'undefined') {
					var old = obj[xml.childNodes[i].nodeName];
					obj[xml.childNodes[i].nodeName] = [];
					obj[xml.childNodes[i].nodeName].push(old);
				}
				obj[xml.childNodes[i].nodeName]
						.push(xml2Json(xml.childNodes[i]));
			}

		}
	}

	return obj;
};

