theGuyTryingToLearn Posted April 9, 2008 Share Posted April 9, 2008 Im following the XML to HTML tutorial on this site, and have hit a wall. (Using FF, XP and DreamWeaver)(This post could possibly go in the javascript forum.)Im trying to display an english word alongside a japanese word. The words are in an XML file, 'japaneseWords.xml' (see below, I copied it there). I can get it to load, (aparently) but it all falls apart in my showXML function :function showXML(){ var x = xmlDoc.getElementsByTagName("WORD"); alert(xmlDoc); //alert box says '[Object XMLDocument]' alert(x + '=x ' + x.length + '=length'); //alert box says '[Object HTMLCollection]=x 0=length' for(var a = 0; a < x.length; x++) { document.write(x[a].getElementsByTagName("ENGLISH")[0].childNodes[0].nodeValue); } }And heres the XML file:<?xml version="1.0" encoding="iso-8859-1"?><MYXML><WORD> <ENGLISH>hello</ENGLISH> <JAPANESE>konnichiwa</JAPANESE></WORD><WORD> <ENGLISH>how are you</ENGLISH> <JAPANESE>o-genki desu ka</JAPANESE></WORD><WORD> <ENGLISH>fine, thank you</ENGLISH> <JAPANESE>genki desu</JAPANESE></WORD><WORD> <ENGLISH>my name is ________</ENGLISH> <JAPANESE>watashi no namae wa _______ desu</JAPANESE></WORD></MYXML> Link to comment Share on other sites More sharing options...
boen_robot Posted April 9, 2008 Share Posted April 9, 2008 x is the root element of the XML - "MYXML". The child nodes of this elements are stored in its "childNodes" property, not the node itself. Try this: alert(x + '=x ' + x.childNodes.length + '=length');for(var a = 0; a < x.childNodes.length; x++){document.write(x.childNodes[a].getElementsByTagName("ENGLISH")[0].childNodes[0].nodeValue);} Link to comment Share on other sites More sharing options...
theGuyTryingToLearn Posted April 9, 2008 Author Share Posted April 9, 2008 x is the root element of the XML - "MYXML". The child nodes of this elements are stored in its "childNodes" property, not the node itself. Try this:alert(x + '=x ' + x.childNodes.length + '=length');for(var a = 0; a < x.childNodes.length; x++){document.write(x.childNodes[a].getElementsByTagName("ENGLISH")[0].childNodes[0].nodeValue);} Somethings still not right. code in question :var x = xmlDoc.getElementsByTagName("WORD");alert('x = ' + x);//x = Object [HTML Collection]'for(var a = 0; a < 3; x++){document.write(x.childNodes[a].getElementsByTagName("ENGLISH")[0].childNodes[0].nodeValue);//above spits out error in firebug : 'x.childNodes has no properties'} Link to comment Share on other sites More sharing options...
theGuyTryingToLearn Posted April 9, 2008 Author Share Posted April 9, 2008 OMG im retarded.....[edit] explanation of why Im retarded : given for(var a = 0; a < x.childNodes.length; x++), why am I incrementing x? Still, after all this, it dosent work. I think Ive narrowed it down to these lines : var x = xmlDoc.getElementsByTagName("WORD"); alert(x); //output is '[object HTML Collection]' alert(x.length);//output is 0 Makes me think xmlDoc isnt doing what its s'posed to. Link to comment Share on other sites More sharing options...
Synook Posted April 10, 2008 Share Posted April 10, 2008 Is xmlDoc a global variable? Link to comment Share on other sites More sharing options...
jesh Posted April 10, 2008 Share Posted April 10, 2008 What gets alerted when you do this? var x = xmlDoc.getElementsByTagName("WORD");alert(x.childNodes.length); Link to comment Share on other sites More sharing options...
theGuyTryingToLearn Posted April 10, 2008 Author Share Posted April 10, 2008 What gets alerted when you do this?var x = xmlDoc.getElementsByTagName("WORD");alert(x.childNodes.length); Yes xmlDoc is global. That code generates a error in firebug : x.childNodes has no propertiesStill playing w it. Link to comment Share on other sites More sharing options...
jesh Posted April 10, 2008 Share Posted April 10, 2008 Bah, I should have seen this earlier...getElementsByTagName returns an array of elements. If you want to get the child nodes of the elements in the array, you'd first have to loop through the array and then attempt to access the child nodes. var x = xmlDoc.getElementsByTagName("WORD");for(var i = 0; i < x.length; i++){ alert(x[i].childNodes.length);} It might even be more understandable if you renamed your variable from "x" to something like "words". var words = xmlDoc.getElementsByTagName("WORD");for(var i = 0; i < words.length; i++){ alert(words[i].childNodes.length);} EDIT: If that still isn't working, you'll want to post the code that defines "xmlDoc". Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.