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>
 
Discussioni simili
Autore Titolo Forum Risposte Data
L problema con query select PHP 2
T problema con select dinamica con jquery Javascript 0
M [PHP] Problema con query select PHP 2
webmachine [PHP][MYSQL] Problema con le SELECT PHP 5
H problema con select dinamiche e javascript Javascript 0
A Problema recupero valori da select con chiamata ajax Ajax 1
H Problema con MySQL e PHP, dopo aver fatto una SELECT non ottengo alcun risultato PHP 7
S Problema con inserire la select in un array PHP 2
E problema con select dinamica - doctype - ie ASP.NET 4
S Problema con 2 funzioni:array,select dinamica Javascript 21
T Problema con select dinamica Javascript 2
M problema con il tag <select> PHP 3
F Problema select dinamiche con php e jquery jQuery 1
C Problema con il select PHP 1
M problema con select jquery e php PHP 0
A Problema: creare nuove "option" di una select-list con javascript Javascript 1
T [php] problema creazione query select-where PHP 5
webmachine [PHP] Problema: Stampare tutte le occorrenza di una SELECT PHP 2
F Problema attivare/disattivare immagine calendario datepicker da select jQuery 1
P problema: la select della città mi va a fondo pagina invece di stare a fianco PHP 7
I problema <select> PHP 0
R Problema Select Php PHP 0
F Problema selezione dinamica select/jsp/mysql Javascript 0
P Problema multi select form jQuery 6
Virginia86 [risolto] Problema select e verifica form PHP 38
B problema select PHP 3
L problema option select senza database PHP 2
E problema di sintassi su una select (plugin) PHP 5.3 PHP 3
EffeElle Php problema query select PHP 5
P problema passaggio valore javascript a select su pagine php Javascript 0
C Problema SELECT, AND, OR PHP 2
S Problema gestione select multipla Javascript 0
M problema select autoaggiornata Ajax 0
S Problema select su recordset nidificati Classic ASP 5
U Come risolvo questo problema col pulsante SELECT?? PHP 1
P Problema UNION ALL SELECT PHP 3
M Select scelta sottofondo - problema firefox Javascript 1
R Problema su select Javascript 1
I Sto progettando nuovi siti utilizzando bootstrap e devo dire funziona bene, l'unico problema e la maschera -moz- HTML e CSS 0
K Problema form update PHP 2
O problema con dvr dahua xvr5116 IP Cam e Videosorveglianza 0
S Problema nel ciclare un json Javascript 0
G Problema con Xampp Web Server 1
andrea barletta Problema con miniature comandi Photoshop 0
I problema con alice Posta Elettronica 0
K Problema Inner join PHP 1
F firefox problema http Linux e Software 0
N Problema con position absolute e overflow HTML e CSS 4
E Problema jquery Success jQuery 2
L Problema con inner join PHP 11

Discussioni simili