Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML Applications

XML Applications


This chapter demonstrates some small XML applications built with XML, HTML, XML DOM, and JavaScript.


XML document example

In this application, we will use "cd_catalog.xml" files.


to display the first CD in an HTML div element

The following example retrieves XML data from the first CD element and then displays the data in the 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[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);

title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);

year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);

txt="Artist: " + artist + "
Title: " + title + "
Year: "+ year;

document.getElementById("showCD").innerHTML=txt;

}

Try it »


Adding Navigation Scripts

To add navigation (functionality) to the example above, two functions, next() and previous(), need to be created:

Example

function next()

{ // display the next CD, unless you are on the last CD

if (i

{

i++;

displayCD();

}

}

function previous()

{ // displays the previous CD, unless you are on the first CD

if (i>0)

{

i--;

displayCD();

}

}

Try it »


Displaying Album Information When a CD Is Clicked

The final example shows how to display album information when a user clicks a CD item:

Try it。

To learn more about using JavaScript and XML DOM, please visit our XML DOM Tutorial。