Per la lettura dell'XML ti può essere utile questa funzione:
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 /* TRUE is the default value for empty nodes */ vResult = 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 oAlbero = getXMLData(oResponseXML);
// abbiamo ottenuto il nostro albero di oggetti javascript! provare per credere: alert(JSON.stringify(oAlbero));
Creare la funzione dal contenuto dell'XML:
codice:
var miaFunzione = new Function(oAlbero.nodofiglio1.nodofiglio2.etc.etc.action);
miaFunzione();
Oppure, se devi estrarre solo il contenuto di un nodo, è inutile leggere tutto l'XML (quindi dimentica la funzione che ti ho postato!!):
codice:
var miaFunzione = new Function(tuoNodo.nodeValue);
miaFunzione();