XML Applications
- Previous Page AJAX Database
- Next Page AJAX Example
This chapter demonstrates an HTML application using XML, HTTP, DOM, and JavaScript.
used XML document
In this chapter, we will use the document named "music_list.xmlof the XML file.
Display XML data in an HTML table
This example traverses each <TRACK> element and then displays the values of the <ARTIST> and <TITLE> elements in an HTML table:
Example
<html> <body> <table id="demo"></table> <script> function loadXMLDoc() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xmlhttp.open("GET", "music_list.xml", true); xmlhttp.send(); } function myFunction(xml) { var i; var xmlDoc = xml.responseXML; var table="<tr><th>Artist</th><th>Track</th></tr>"; var x = xmlDoc.getElementsByTagName("TRACK"); for (i = 0; i < x.length; i++) { table += "<tr><td>" + x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue + "</td><td>" + x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + "</td></tr>"; } document.getElementById("demo").innerHTML = table; } </script> </body> </html>
For more information on using JavaScript and XML DOM, please visit DOM Introduction.
Display the first song in the HTML div element
This example uses a function to display the first song in the HTML element with id="displayMUSIC":
Example
displayMUSIC(0); function displayMUSIC(i) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this, i); } }; xmlhttp.open("GET", "music_list.xml", true); xmlhttp.send(); } function myFunction(xml, i) { var xmlDoc = xml.responseXML; x = xmlDoc.getElementsByTagName("TRACK"); document.getElementById("showMUSIC").innerHTML = "<ul>" + "<li>Track: " + x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + "</li>" "<li>Artist: " + x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue + "</li>" + "<li>Album: " + x[i].getElementsByTagName("ALBUM")[0].childNodes[0].nodeValue + "</li>" + "<li>Country: " + x[i].getElementsByTagName("COUNTRY")[0].childNodes[0].nodeValue + "</li>" + "<li>Company: " + x[i].getElementsByTagName("COMPANY")[0].childNodes[0].nodeValue + "</li>" + "<li>Year: " + x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue + "</li>" + "</ul>"; }
Navigate between tracks
To navigate between tracks in the above example, add the next() and previous() functions:
Example
function next() { // Display the next song, unless it is the last one if (i < x.length-1) { i++; displayMUSIC(i); } } function previous() { // Display the previous song, unless it is the first one if (i > 0) { i--; displayMUSIC(i); } }
Display album information when clicking on a song
This last example demonstrates how to display album information when a user clicks on a song:
Example
function displayMUSIC(i) { document.getElementById("showMUSIC").innerHTML = "<ul>" + "<li>Track: " + x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + "</li>" "<li>Artist: " + x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue + "</li>" + "<li>Album: " + x[i].getElementsByTagName("ALBUM")[0].childNodes[0].nodeValue + "</li>" + "<li>Country: " + x[i].getElementsByTagName("COUNTRY")[0].childNodes[0].nodeValue + "</li>" + "<li>Company: " + x[i].getElementsByTagName("COMPANY")[0].childNodes[0].nodeValue + "</li>" + "<li>Year: " + x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue + "</li>" + "</ul>"; }
- Previous Page AJAX Database
- Next Page AJAX Example