Tu stai cercando di parsare in json un foglio XML! Se vuoi trasformare un xml in json devi fare qualcosa del genere:
Prova con questo codice e vedi se è quello che vuoi. Poi per jQueryizzarlo ci si mette un attimo…codice:function buildValue(sValue) { if (/^\s*$/.test(sValue)) { return null; } if (/^(true|false)$/i.test(sValue)) { return sValue.toLowerCase() === "true"; } if (isFinite(sValue)) { return parseFloat(sValue); } if (isFinite(Date.parse(sValue))) { return new Date(sValue); } return sValue; } function getXMLData (oXMLParent) { var vResult = /* true is the default value for empty nodes */ true, nLength = 0, sTxtContent = ""; if (oXMLParent.hasAttributes()) { vResult = {}; for (nLength; nLength < oXMLParent.attributes.length; nLength++) { iAttrib = oXMLParent.attributes.item(nLength); vResult["@" + iAttrib.nodeName.toLowerCase()] = buildValue(iAttrib.nodeValue.replace(/^\s+|\s+$/g, "")); } } if (oXMLParent.hasChildNodes()) { var iKey, iValue, iXMLChild; for (var iChildId = 0; iChildId < oXMLParent.childNodes.length; iChildId++) { iXMLChild = oXMLParent.childNodes.item(iChildId); if (iXMLChild.nodeType === 1 && !iXMLChild.prefix) { if (nLength === 0) { vResult = {}; } iKey = iXMLChild.nodeName.toLowerCase(); iValue = getXMLData(iXMLChild); if (vResult.hasOwnProperty(iKey)) { if (vResult[iKey].constructor !== Array) { vResult[iKey] = [vResult[iKey]]; } vResult[iKey].push(iValue); } else { vResult[iKey] = iValue; nLength++; } } else if (iXMLChild.nodeType === 3) { sTxtContent += iXMLChild.nodeValue.replace(/^\s+|\s+$/g, ""); } else if (iXMLChild.nodeType === 4) { sTxtContent += iXMLChild.nodeValue; } } } if (nLength > 0) { vResult.keyValue = buildValue(sTxtContent); /* Object.freeze(vResult); */ } else if (sTxtContent) { vResult = buildValue(sTxtContent); } return vResult; } var miaVariabile; oXHR = new XMLHttpRequest(); oXHR.onreadystatechange = function() { if (oXHR.readyState === 4) { miaVariabile = getXMLData(this.responseXML); } }; oXHR.open("GET", "http://localhost/gaia/test_xml", true); oXHR.send(null); alert(JSON.stringify(miaVariabile));![]()

Rispondi quotando