tags:

views:

73

answers:

1

Hi,

When I try to traverse to the children of the gallery node, it return nothing for the nodevalue, when it should read 'Joe Bloggs', and '#text' for the node name when it should read 'property'. Can anyone see what might be wrong with my javascript or XML.

<gallery_index>
  <gallery id="0">
     <property id="name">Joe Bloggs</property>
     <property id="description"><p>testtest</p></property>
     <property id="thumbnail_path">/images/thumb.jpg</property>
     <property id="weblink">http://www.cnn.com&lt;/property&gt;
     <property id="client"></property>
     <assets/>
  </gallery>
</gallery_index>

Javascript.

// code for IE
 if (window.ActiveXObject){
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
 }
 // code for Mozilla, Firefox, Opera, etc.
 else if (document.implementation.createDocument)
 {
 xmlDoc=document.implementation.createDocument("","",null);
 }
 else
 {
 alert('Your browser cannot handle this script');
 }
 xmlDoc.async=false;
 xmlDoc.load("../../../../../../../../xml/gallery_index.xml");

 galleryIndexNode = xmlDoc.documentElement;
 galleryNode = galleryIndexNode.firstChild;

 alert(galleryNode.firstChild.nodeName);

EDIT:

OK, changed the following javascript. The nodeName and nodeValue will display if I do this.

alert(galleryNode.childNodes[1].nodeName);

. . . . .

alert(galleryNode.childNodes[3].nodeName);

Is my XML badly formed? It displays in FF fine. What can i do to fix this?

+1  A: 

The first child of <gallery> in your example is a text node with nothing in it (or a line break and some tabs, depending on whether whitespace is preserved by your parser). The element containing Joe Bloggs is the second child of <gallery>.

In fact, by the look of it, galleryNode in your script points to the first text child of <gallery_index>, rather than the <gallery> element.

Paul Butcher
i dont if you could make me anymore confused.
madphp
Think i see what you mean now. Sorrt. I made changes.
madphp
There is nothing wrong with the XML, this is just the way the DOM works. If you wish to perform an action using all child elements, but ignoring all other types of node, you should check each child for a nodeType of 1 (ELEMENT_NODE) as you loop over them.
Paul Butcher