Ciao a tutti...
ho un file XML così fatto:che vado a leggere e visualizzare in questo modo con javascript:codice:<?xml version="1.0" ?> <root> <elementi> <item> <etichetta>Alessandria</etichetta> <valore>AL</valore> </item> <item> <etichetta>Torino</etichetta> <valore>TO</valore> </item> <item> <etichetta>Cuneo</etichetta> <valore>CN</valore> </item> </elementi> </root>Fin qui tutto ok... ma a me servirebbe riuscire a fare un a funzione unica (e senza variabili esterne) che mi prelevi il file e me lo visualizzasse. Per funzione unica intendo un qualcosa che può richiamare anche altre funzioni, ma che alla fine del ciclo non abbia lasciato variabili instanziate... in modo da poterla richiamare più volte senza che si corra il rischio di interferire tra una chiamata e l'altra.codice:<html> <head> <title>Untitled</title> <script language="JavaScript" src="objTendine.js"></script> <style> .input{ color:red; } </style> <script language="JavaScript"> <!-- var XMLFile = "province.xml" var xml = null; var arrDati = new Array(); function vis(a){ try{ if (document.implementation && document.implementation.createDocument){ xml = document.implementation.createDocument("","",null) xml.onload=Leggi xml.load(a); }else if (window.ActiveXObject){ xml = new ActiveXObject("Microsoft.XMLDOM") xml.onreadystatechange = IEGo; xml.load(a); } }catch(e){ alert("Errore nella funzione vis().\nERRORE: "+e) } } function IEGo(){ if (xml.readyState == 4) Leggi(); } function Leggi(){ try{ var Nodo = xml.getElementsByTagName("elementi")[0].getElementsByTagName("item"); LeggiArrDati(Nodo); var testo = "dati: "; for (var i = 0 ; i < arrDati.length ; i++ ) testo += arrDati[i].label+" "; document.getElementById("visDIV").innerHTML=testo; }catch(e){ alert("Errore nella funzione Leggi().\nERRORE: "+e) } } function LeggiArrDati(obj){ for (i = 0 ; i < obj.length ; i++) arrDati[i] = new dati(getValue(obj[i],"etichetta"), getValue(obj[i],"valore")); } function getValue(vobj,tag,param){ if (param) return vobj.getElementsByTagName(tag)[0].getAttribute(param); else return vobj.getElementsByTagName(tag)[0].childNodes[0].nodeValue; } function dati(a,b){ this.label=a; this.valore=b; } //--> </script> </head> <body onLoad="vis(XMLFile)"> <div id="visDIV"></div> </body> </html>
Sapete aiutarmi? è tutto il giorno che ci provo e non so più come mettere queste linee di codice!