Note that there are some explanatory texts on larger screens.

plurals
  1. POXML & Javascript: cannot get this simple demo to work
    text
    copied!<p>I'm trying to loop through an XML file and print out the results in a table. Now I have an example from W3C Schools which is very similar and it works, but substituting my file and values stops it working. I would appreciate it someone could point me to what I am dong wrong.</p> <p>So this is the W3C example in full: </p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;script type="text/javascript"&gt; 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","cd_catalog.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; document.write("&lt;table border='1'&gt;"); var x=xmlDoc.getElementsByTagName("CD"); for (i=0;i&lt;x.length;i++) { document.write("&lt;tr&gt;&lt;td&gt;"); document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); document.write("&lt;/td&gt;&lt;td&gt;"); document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); document.write("&lt;/td&gt;&lt;/tr&gt;"); } document.write("&lt;/table&gt;"); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>My code differs as follows:</p> <pre><code>xmlhttp.open("GET","data.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; document.write("&lt;table border='1'&gt;"); var x=xmlDoc.getElementsByTagName("festival"); for (i=0;i&lt;x.length;i++) { document.write("&lt;tr&gt;&lt;td&gt;"); document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue); document.write("&lt;/td&gt;&lt;td&gt;"); document.write(x[i].getElementsByTagName("artist")[0].childNodes[0].nodeValue); document.write("&lt;/td&gt;&lt;/tr&gt;"); } </code></pre> <p>and the two XML files are at <a href="http://www.holtworth.net/xml/data.xml" rel="nofollow noreferrer">http://www.holtworth.net/xml/data.xml</a> (my XML) and <a href="http://www.holtworth.net/xml/cd_catalog.xml" rel="nofollow noreferrer">http://www.holtworth.net/xml/cd_catalog.xml</a> (original)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload