function xml2json(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;
}

    function TextToXML(strXML) {  
    var xmlDoc = null;  
     try {  
      xmlDoc = (document.all)?new ActiveXObject("Microsoft.XMLDOM"):new DOMParser();  
      xmlDoc.async = false;  
    } catch(e) {throw new Error("XML Parser could not be instantiated");}  
     var out;  
     try {  
    if(document.all) {  
      out = (xmlDoc.loadXML(strXML))?xmlDoc:false;  
     } else {    
      out = xmlDoc.parseFromString(strXML, "text/xml");  
    }  
    } catch(e) { throw new Error("Error parsing XML string"); }  
    return out;  
   }   