melsi Posted February 17, 2012 Share Posted February 17, 2012 i have this xml file which i created for test purposes http://betjeeves.com/test.xmlI cant seem to figure out how to parse the data and which would be the best way to do it, i've looked at using Javascript but havnt ad any luck is this code useless? <script type="text/javascript">if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.open("GET","odds_en.xml",false);xmlhttp.send();xmlDoc=xmlhttp.responseXML;document.write("<table border='1'>");var x=xmlDoc.getElementsByTagName("bets");for (i=0;i<x.length;i++) { document.write("<tr><td>"); document.write(x[i].getElementsByTagName("choice name")[0].childNodes[0].nodeValue); document.write("</td><td>"); document.write(x[i].getElementsByTagName("odd")[0].childNodes[0].nodeValue); document.write("</td></tr>"); }document.write("</table>");</script> I just want to be able to sort that xml data into a neat html table that displays the teams that are playing eachother and the odds.Please somebody help me from pulling my hair out Thanks Link to comment Share on other sites More sharing options...
boen_robot Posted February 17, 2012 Share Posted February 17, 2012 The filename seems different, but I assume you know THAT.The getElementsByTagName() is supposed to, as its name suggests, get elements. There is no "choice name" element. Instead, you have a "choice" element, with an attribute called "name".If you want to get the attribute "name" of the first "choice" element within the "i"th "bets" element, you should replace document.write(x[i].getElementsByTagName("choice name")[0].childNodes[0].nodeValue); with document.write(x[i].getElementsByTagName("choice")[0].getAttribute("name")); Link to comment Share on other sites More sharing options...
melsi Posted February 17, 2012 Author Share Posted February 17, 2012 The filename seems different, but I assume you know THAT. The getElementsByTagName() is supposed to, as its name suggests, get elements. There is no "choice name" element. Instead, you have a "choice" element, with an attribute called "name". If you want to get the attribute "name" of the first "choice" element within the "i"th "bets" element, you should replacedocument.write(x[i].getElementsByTagName("choice name")[0].childNodes[0].nodeValue); with document.write(x[i].getElementsByTagName("choice")[0].getAttribute("name")); thanks for you response! I tried that but it still doesnt seem to be working see here - http://www.betjeeves.com/test I want it to show the teams playing eachother and the odds in a table if thats possible? thanks for you help Link to comment Share on other sites More sharing options...
boen_robot Posted February 17, 2012 Share Posted February 17, 2012 It appears "odd" is an attribute as well... so document.write(x[i].getElementsByTagName("odd")[0].childNodes[0].nodeValue); should become document.write(x[i].getElementsByTagName("choice")[0].getAttribute("odd")); If you want to loop over all choice elements, you'll have to store the output of 'getElementsByTagName("choice")' in the same way you do with "bets". Link to comment Share on other sites More sharing options...
melsi Posted February 17, 2012 Author Share Posted February 17, 2012 It appears "odd" is an attribute as well... sodocument.write(x[i].getElementsByTagName("odd")[0].childNodes[0].nodeValue); should become document.write(x[i].getElementsByTagName("choice")[0].getAttribute("odd")); If you want to loop over all choice elements, you'll have to store the output of 'getElementsByTagName("choice")' in the same way you do with "bets". Ok thanks, excuse my ignorance but would you mind explaining in a little more detail how i would do that? Link to comment Share on other sites More sharing options...
melsi Posted February 17, 2012 Author Share Posted February 17, 2012 im now achieving this - http://www.betjeeves.com/test using this: <script type="text/javascript">if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.open("GET","test.xml",false);xmlhttp.send();xmlDoc=xmlhttp.responseXML;document.write("<table border='1'>");var x=xmlDoc.getElementsByTagName("match");for (i=0;i<x.length;i++) { document.write("<tr><td>"); document.write(x[i].getElementsByTagName("bet")[0].getAttribute("name")); document.write("</td><td>"); document.write(x[i].getElementsByTagName("choice")[0].getAttribute("odd")); document.write("</td></tr>"); }document.write("</table>");</script> How can i add an extra column to the table with the team names? thanks Link to comment Share on other sites More sharing options...
boen_robot Posted February 17, 2012 Share Posted February 17, 2012 The line var x=xmlDoc.getElementsByTagName("bets"); creates a variable called "x" and makes it store all "bets" elements across the document.The very next line for (i=0;i<x.length;i++) creates a variable called "i" that has the number "0", and executes the block following it as long as "i" is less than the length of "x" (i.e. the number of "bets" elements in the document). Every time the block is executed, "i" will increase its value, so eventually (when there are no more "bets" elements"), the block will no longer execute.Within the block itself, you can use x[i] to get the "bets" element that's "i"th in the collection (first the element at index 0, then the element at index 1, etc.).Within that block, you can also perform the very same thing - have another variable storing a collection of certain elements, and loop over them with another "for" adjusted for this other collection. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.