Problema Select con Javascript e nodi Dom XML.

lucaskain

Nuovo Utente
14 Giu 2013
3
0
0
Salve ragazzi, avrei un problema con il codice javascript in correlazione al file xml. Vorrei in pratica che una form composta da 3 select che ho creato all'interno di un file HTML prendessero rispettivamente gli attributi collegati ai nodi delle città con tag <city>, e le ultime 2 select gli hotel presenti nella città selezionata. Mi da quest'errore:


"nodo.getAttribute("namecity") is not a function"

e lo stesso con le altre select (hotel 1 , hotel 2).


Vi allego il file Javascript e il file xml a cui si riferisce.

File Js:


Codice:
//carico parser XML;

function caricaXML(NomeFile){
	var xmlhttp;
	if(window.XMLHttpRequest) {
		// IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else{
		// IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET", NomeFile, false);
	xmlhttp.send();
	return xmlhttp.responseXML;
}

//creo oggetto contenitore e i suoi metodi;

function contenitore(){
	this.citta = new Array();
	this.nomecitta = new Array();
	this.inizializza = 
		function(nomeFile){
			var doc = caricaXML(nomeFile);
			var l = doc.getElementsByTagName("city");
			for(i in l){ //controllare in caso di errori.
				var c = new citta();
				c.inizializza(l[i]);
				this.citta.push(c);
				var j=0;
				while((j < this.nomecitta.length) && (this.nomecitta[j].namecity != c.namecity)){
					j++
				}
				if (j == this.nomecitta.length){
					this.nomecitta.push(c.namecity);
				}
			}
		}
    this.selectcitta=
		function(){
		var s = "";
		s += '<option value="" selected="selected">' + 'seleziona un gruppo</option>'
		var l = Ilcontenitore.nomecitta;
		for (i in l){
			s += '<option value="' + l[i] + '">' + l[i] + '</option>';
		}
		return s;
	}
}


//creo oggetto città

function citta(){
	this.namecity;
	this.hotel = new Array();
	this.inizializza = 
		function(nodo){
			this.namecity = nodo.getAttribute("namecity");
			var c= nodo.getElementsByTagName("hotel");
			for (i in c){
				var h = new hotel();
				h.inizializza(c[i]);
				this.hotel.push(h);
			}
		}
}

//creo oggetto hotel;

function hotel(){
	this.name;
	this.price;
	this.star;
	this.internet;
	this.tv;
	this.spa;
	this.inizializza =
		function(nodo){
			var h;
			h = nodo.getElementsByTagName("name");
			this.name = h[0].childNodes[0].nodeValue;
			h = nodo.getElementsByTagName("price");
			this.price = h[0].childNodes[0].nodeValue;
			h = nodo.getElementsByTagName("star");
			this.star = h[0].childNodes[0].nodeValue;
			h = nodo.getElementsByTagName("internet");
			this.internet = h[0].childNodes[0].nodeValue;
			h = nodo.getElementsByTagName("tv");
			this.tv = h[0].childNodes[0].nodeValue;
			h = nodo.getElementsByTagName("spa");
			this.spa = h[0].childNodes[0].nodeValue;
		}
		
}


var Ilcontenitore; // variabile globale;

function inizializza(){
	Ilcontenitore = new contenitore();
	Ilcontenitore.inizializza("alternativo.xml");
	var nodo = document.getElementById("select1");
	nodo.innerHTML = h.primaselect;
}

window.onload = inizializza; // al caricamento della pagina esegue funzione inizializza

file XML:


Codice:
<?xml version="1.0" ?>
<contenitore>
	<city namecity="Pisa">
		<hotel> 
			<name>San Ranieri</name> 
			<star>4</star> 
			<price>70€</price> 
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Grand Hotel Duomo</name> 
			<star>4</star> 
			<price>75€</price> 
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>no</tv>
		</hotel>
		<hotel>
			<name>Hotel Moderno</name> 
			<star>2</star> 
			<price>35€</price>
			<internet>no</internet> 
			<spa>no</spa> 
			<tv>no</tv>
		</hotel>
        <hotel> 
			<name>Hotel Ruggero</name> 
			<star>3</star> 
			<price>35€</price> 
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Relais dell'orologio</name> 
			<star>5</star> 
			<price>125€</price> 
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
	</city>
	<city namecity="Livorno">
		<hotel>
			<name>Hotel Etrusconia</name> 
			<star>1</star> 
			<price>25€</price>
			<internet>no</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Hotel Touring</name> 
			<star>3</star> 
			<price>45€</price>
			<internet>no</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel>
			<name>Giappone Inn</name> 
			<star>3</star> 
			<price>42€</price>
			<internet>no</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Hotel Boston</name> 
			<star>3</star> 
			<price>40€</price> 
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>no</tv>
		</hotel>
		<hotel>
			<name>Hotel Rex</name> 
			<star>4</star> 
			<price>80€</price>
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
	</city>
	<city namecity="Firenze">
		<hotel> 
			<name>Hotel Cimabue</name> 
			<star>3</star> 
			<price>75€</price>
			<internet>yes</internet> 
			<spa>yes</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Hotel David</name> 
			<star>3</star> 
			<price>75€</price> 
			<internet>no</internet>
			<spa>yes</spa> 
			<tv>no</tv>
		</hotel>
		<hotel> 
			<name>Golden Tower</name> 
			<star>5</star> 
			<price>150€</price>
			<internet>yes</internet> 
			<spa>yes</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Bellariva</name> 
			<star>2</star> 
			<price>45€</price> 
			<internet>no</internet> 
			<spa>no</spa> 
			<tv>no</tv>
		</hotel>
		<hotel> 
			<name>Hotel-pensione Ferretti</name> 
			<star>1</star> 
			<price>18</price> 
			<internet>no</internet> 
			<spa>no</spa> 
			<tv>no</tv>
		</hotel>
	</city>
	<city namecity="Grosseto">
		<hotel> 
			<name>Fattoria Maremmana</name> 
			<star>3</star> 
			<price>83€</price> 
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Grand Hotel Bastioni</name> 
			<star>4</star> 
			<price>143€</price> 
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Hotel Airone</name> 
			<star>4</star> 
			<price>136€</price> 
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Hotel Appennino</name> 
			<star>1</star> 
			<price>21€</price> 
			<internet>no</internet> 
			<spa>no</spa> 
			<tv>no</tv>
		</hotel>
		<hotel> 
			<name>Le Macinaie</name> 
			<star>2</star> 
			<price>35€</price> 
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
	</city>
	<city namecity="Siena">
		<hotel> 
			<name>Hotel Minerva</name> 
			<star>3</star> 
			<price>72€</price>
			<internet>no</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Hotel Italia</name> 
			<star>3</star> 
			<price>75€</price> 
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Hotel Athena</name> 
			<star>4</star> 
			<price>120€</price>
			<internet>no</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
		<hotel> 
			<name>Hotel Garden</name> 
			<star>4</star> 
			<price>135€</price>
			<internet>no</internet> 
			<spa>yes</spa> 
			<tv>no</tv>
		</hotel>
		<hotel> 
			<name>Da Vestro</name> 
			<star>2</star> 
			<price>40€</price> 
			<internet>yes</internet> 
			<spa>no</spa> 
			<tv>yes</tv>
		</hotel>
	</city>
</contenitore>