Jump to content

XML to an HTML table


bosoxbill

Recommended Posts

I'm trying to load an XML file into a table. Each row (<tr>) has 11 stats (<td>). When I run my code, I only get the first td (the player's name). Could someone point me in the right direction? Here is my sample XML data: <nitf xmlns="http://ap.org/schemas/03/2005/nitf"> <table> <tr> <td>Player</td> <td>G</td> <td>MIN</td> <td>FGM-FGA</td> <td>PCT</td> <td>FGM-FGA</td> <td>FTM-FTA</td> <td>PCT</td> <td>PTS</td> <td>AVG</td> <td>HG</td> </tr> <tr> <td>Griffin</td> <td>60</td> <td>32.4</td> <td>447-818</td> <td>.546</td> <td>3-16</td> <td>216-328</td> <td>.659</td> <td>1113</td> <td>18.6</td> <td>31</td> </tr> <tr> <td>Crawford</td> <td>58</td> <td>29.5</td> <td>347-791</td> <td>.439</td> <td>111-289</td> <td>182-212</td> <td>.858</td> <td>987</td> <td>17.0</td> <td>30</td> </tr> <tr> <td>Paul</td> <td>50</td> <td>32.8</td> <td>284-600</td> <td>.473</td> <td>59-175</td> <td>193-216</td> <td>.894</td> <td>820</td> <td>16.4</td> <td>30</td> </tr> <tr> <td>Barnes</td> <td>60</td> <td>25.9</td> <td>233-509</td> <td>.458</td> <td>83-239</td> <td>70-94</td> <td>.745</td> <td>619</td> <td>10.3</td> <td>21</td> </tr> <tr> <td>Butler</td> <td>59</td> <td>24.3</td> <td>226-532</td> <td>.425</td> <td>90-238</td> <td>65-79</td> <td>.823</td> <td>607</td> <td>10.3</td> <td>33</td> </tr> <tr> <td>Bledsoe</td> <td>61</td> <td>21.3</td> <td>219-486</td> <td>.451</td> <td>25-58</td> <td>96-121</td> <td>.793</td> <td>559</td> <td>9.2</td> <td>27</td> </tr> </table></nitf> Here is my HTML: <!DOCTYPE html><html><body> <script>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","lacstats.xml",false);xmlhttp.send();xmlDoc=xmlhttp.responseXML; document.write("<table border='1'>");var x=xmlDoc.getElementsByTagName("tr");for (i=0;i<x.length;i++) { document.write(x.length); document.write("<tr><td>"); document.write(x.getElementsByTagName("td")[0].childNodes[0].nodeValue); document.write("</td><td>"); document.write(x.getElementsByTagName("td")[1].childNodes[1].nodeValue); document.write("</td><td>"); document.write(x.getElementsByTagName("td")[2].childNodes[2].nodeValue); document.write("</td><td>"); document.write(x.getElementsByTagName("td")[3].childNodes[3].nodeValue); document.write("</td><td>"); document.write(x.getElementsByTagName("td")[4].childNodes[4].nodeValue); document.write("</td><td>"); document.write(x.getElementsByTagName("td")[5].childNodes[5].nodeValue); document.write("</td><td>"); document.write(x.getElementsByTagName("td")[6].childNodes[6].nodeValue); document.write("</td><td>"); document.write(x.getElementsByTagName("td")[7].childNodes[7].nodeValue); document.write("</td><td>"); document.write(x.getElementsByTagName("td")[8].childNodes[8].nodeValue); document.write("</td><td>"); document.write(x.getElementsByTagName("td")[9].childNodes[9].nodeValue); document.write("</td><td>"); document.write(x.getElementsByTagName("td")[10].childNodes[10].nodeValue); document.write("</td></tr>"); }document.write("</table>");</script> </body></html>

Link to comment
Share on other sites

For each td element, all the text is stored in the "0th" childnode. As soon as you tried to access childNodes[1].nodeValue, the code broke, because there is no "1st" childNode.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...