Un po' di esempi.... se t'interessa ottenere solo il file XML e caricarlo in una variabile, ti può essere utile questo:
codice:
<!doctype html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title>Leggere file XML</title>
<script type="text/javascript">
function stateChange() {
if (this.readyState === 4) { this._fCallback.apply(this, this._aArgs); }
}
function XHR(sURL, fCallback /*, argumentToPass1, argumentToPass2, etc. */) {
var oResp = new XMLHttpRequest();
if (fCallback) {
oResp._fCallback = fCallback;
oResp._aArgs = Array.prototype.slice.call(arguments, 2);
oResp.onreadystatechange = stateChange;
}
oResp.open("GET", sURL, true);
oResp.setRequestHeader("Content-Type", "text/plain");
oResp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
oResp.send(null);
}
function myFunction() {
var oMyObject = this.responseXML; // la tua variabile
alert(oMyObject);
}
</script>
</head>
<body>
<span style="text-decoration: underline; cursor: pointer;" onclick="XHR('a.xml', myFunction);">Clicca qui</span></p>
</body>
</html>
Questa l'avevo scritta per Mozilla Developer Center... converte il contenuto di un file XML in un albero di oggetti javascript, rispettando la convenzione di Parker. Se non vuoi che l'oggetto generato sia in sola lettura, devi eliminare la chiamata Object.freeze(vResult);...:
codice:
<!doctype html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title>XML to JavaScript Object conversion example</title>
<script type="text/javascript">
function stateChange() {
if (this.readyState === 4) { this._fCallback.apply(this, this._aArgs); }
}
function XHR(sURL, fCallback /*, argumentToPass1, argumentToPass2, etc. */) {
var oResp = new XMLHttpRequest();
if (fCallback) {
oResp._fCallback = fCallback;
oResp._aArgs = Array.prototype.slice.call(arguments, 2);
oResp.onreadystatechange = stateChange;
}
oResp.open("GET", sURL, true);
oResp.setRequestHeader("Content-Type", "text/plain");
oResp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
oResp.send(null);
}
function buildValue (sValue) {
if (sValue.search(/^\s*$/) > -1) { return(null); }
else if (sValue.search(/^(true|false)$/i) > -1) { return(sValue.toLowerCase() === "true"); }
else if (isFinite(sValue)) { return(parseFloat(sValue)); }
else if (isFinite(Date.parse(sValue))) { return(new Date(sValue)); }
else { return(sValue); }
}
function getXMLData (oXMLParent) {
var 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);
}
function myFunction() {
var myFile = this.responseXML; // il file XML...
var oMyObject = getXMLData(myFile); // il contenuto del file XML convertito in un albero di oggetti javascript...
alert(JSON.stringify(oMyObject));
}
</script>
</head>
<body>
<span style="text-decoration: underline; cursor: pointer;" onclick="XHR('a.xml', myFunction);">Clicca qui</span></p>
</body>
</html>
Cia'