XML Applications | Tutorial
Tutorial -- Learning is not just about technology, but also about dreams!
XML Tutorial
XML JavaScript
XML Advanced
XML Applications
This chapter demonstrates some small XML applications built using XML, HTML, XML DOM, and JavaScript.
XML Document Example
In this application, we will use the "cd_catalog.xml" file.
Display the First CD in an HTML div Element
The following example fetches XML data from the first CD element and then displays the data in an HTML element with id="showCD". The displayCD() function is called when the page loads:
Example
x=xmlDoc.getElementsByTagName("CD");
i=0;
function displayCD()
{
artist=(x.getElementsByTagName("ARTIST").childNodes.nodeValue);
title=(x.getElementsByTagName("TITLE").childNodes.nodeValue);
year=(x.getElementsByTagName("YEAR").childNodes.nodeValue);
txt="Artist: " + artist + "<br />Title: " + title + "<br />Year: "+ year;
document.getElementById("showCD").innerHTML=txt;
}
Add Navigation Script
To add navigation (functionality) to the example above, you need to create two functions, next() and previous():
Example
function next()
{ // display the next CD, unless you are on the last CD
if (i<x.length-1)
{
i++;
displayCD();
}
}
function previous()
{ // displays the previous CD, unless you are on the first CD
if (i>0)
{
i--;
displayCD();
}
}
Show Album Information When a CD is Clicked
The final example shows how to display album information when a user clicks on a CD item:
Try it Yourself.For more information about using JavaScript and XML DOM, visit our XML DOM Tutorial.
YouTip