PDA

View Full Version : Problema apertura file XML con JavaScript solo con Internet Explorer


Twincpu
20-07-2006, 13:01
Voglio aprire un file XML con Javascript e Mozilla non mi da nessun problema. Il file XML è un semplice elenco con la seguente struttura:

<page>
<articolo>
<indice>1.</indice>
<testo>Il mio cane</testo>
<trad>(My dog)/trad>
</articolo>
<articolo>
<indice>2.</indice>
<testo>Sono bello</testo>
<trad>(I am beautiful)</trad>
</articolo>
.....
</page>


Ogni articolo a video dovrebbe apparire come una tabella di due righe, la cui prima ha due colonne, una con l'indice e l'altra col testo. La seconda riga invece contiene la trad in corsivo.
Il codice seguente funziona tranquillamente con Mozilla, mentre con Internet Explorer da problemi, ossia non viene visualizzata la trad di ogni articolo.


var flag

function importXML()
{
if (document.implementation && document.implementation.createDocument)
{
flag=1;
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = createTable;
}
else if (window.ActiveXObject)
{
flag=0;
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) createTable()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("articolo.xml");
}

function createTable()
{
var y=0;
var x = xmlDoc.getElementsByTagName('articolo');
var newEl = document.createElement('TABLE');
newEl.setAttribute('id','art');
var tmp = document.createElement('TBODY');
newEl.appendChild(tmp);
for (i=0;i<x.length;i++)
{
var row = document.createElement('TR');
y=0;
for (j=0;j<(x[i].childNodes.length)-flag;j++)
{
y++;
if (x[i].childNodes[j].nodeType != 1) continue;
var container;
if(y<3)
container= document.createElement('TD');
else
container= document.createElement('TR');
if( x[0].childNodes[j].nodeName == 'indice')
container.setAttribute('id','ind');
else if( x[0].childNodes[j].nodeName == 'testo')
container.setAttribute('id','test');
else
container.setAttribute('id','trad');
var theData = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
container.appendChild(theData);
row.appendChild(container);
}
tmp.appendChild(row);
}
document.getElementById('writeroot').appendChild(newEl);
}


Se qualcuno mi riesce ad aiutare ne sarei molto grato....