Bonjour tout le monde

Je cherche � tester l'existence d'un noeud XML avec JavaScript. Voici mon fichier XML :

Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
<apartments>
    <maxID>2</maxID>
    <apartment id="1" cat="1">
      <prices>
        <shared-bedroom>595</shared-bedroom>
      </prices>
    </apartment>
    <apartment id="2" cat="2">
      <prices>
        <shared-bedroom>600</shared-bedroom>
        <single-bedroom>400</single-bedroom>
      </prices>
</apartment>
Comme vous le constatez, les noeuds contenu dans le noeud "prices" peuvent varier.

Je peux avoir un "shared-bedroom". Ou bien encore un "single-bedroom" tout simplement. Ou bien les deux.

C'est pourquoi j'ai besoin de traiter mon XML avec JavaScript pour tester si l'un ou l'autre noeud existe pour mon affichage HTML.

J'ai cr�� le script suivant :


Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
var apartments      = xmlDoc.getElementsByTagName("apartment");
countApartments     = apartments.length;
document.getElementById("apartments").innerHTML = '';
 
 
for(i = 0; i < countApartments; i++) {
 
	/* List prices */
	var sharedBedroom   = apartments[i].getElementsByTagName("shared-bedroom");
	var singleBedroom   = apartments[i].getElementsByTagName("single-bedroom");
	var listPrices 		= '';
 
	if(sharedBedroom.length > 0)	listPrices+= '<li><strong>Price:</strong> from $'+xmlDoc.getElementsByTagName("shared-bedroom")[i].firstChild.nodeValue+'/month/person</li>';
	if(singleBedroom.length > 0)	listPrices+= '<li><strong>Price:</strong> from $'+xmlDoc.getElementsByTagName("single-bedroom")[i].firstChild.nodeValue+'/month</li>';
}
Mais cela ne semble pas fonctionner ! Qu'est-ce qui cloche avec mon code ?

Merci et bonne nuit tout le monde