XML Aplicación
Este capítulo demuestra algunas aplicaciones XML pequeñas construidas con XML, HTML, XML DOM y JavaScript.
Ejemplo de documento XML
En esta aplicación, usaremosarchivo.
Mostrar el primer CD en un elemento div HTML
El siguiente ejemplo obtiene datos XML del primer elemento CD y luego muestra los datos en el elemento HTML con id="showCD". La función displayCD() se llama al cargar la página:
Ejemplo
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 + "<br />Title: " + title + "<br />Year: "+ year;
document.getElementById("showCD").innerHTML=txt;
}
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 + "<br />Title: " + title + "<br />Year: "+ year;
document.getElementById("showCD").innerHTML=txt;
}
Pruébalo »
Agregar script de navegación
Para agregar navegación (funcionalidad) al ejemplo anterior, necesitas crear dos funciones: next() y previous():
Ejemplo
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();
}
}
{ // 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();
}
}
Pruébalo »
Mostrar información del álbum al hacer clic en el CD
El último ejemplo muestra cómo mostrar información del álbum cuando el usuario hace clic en un elemento CD:
。
Para obtener más información sobre el uso de JavaScript y XML DOM, visita nuestroTutorial de XML DOM。
Otras extensiones