Cant traverse XML document
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</property>
<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 = xmlDo开发者_Python百科c.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?
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.
精彩评论