Friday, September 5, 2008

importng xml file and displaying in table using javascript

//html



// JScript File
var xmlDoc;

function importXML()
{
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = createTable;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) createTable()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("http:path.xml")

}

function createTable()
{
debugger;
var x = xmlDoc.getElementsByTagName('item');

var tmp = document.createElement('TBODY');

for (j=0;j {
var row = document.createElement('TR');
tmp.appendChild(row);

var vtitles = x[j].getElementsByTagName("title");
var vlinks = x[j].getElementsByTagName("link");
var vdesc = x[j].getElementsByTagName("description");
var tdTitle = document.createElement('TD');
row.appendChild(tdTitle);

var tddesc = document.createElement('TD');
row.appendChild(tddesc);
var valink = document.createElement('A');
tdTitle.appendChild(valink);
valink.title = vtitles[0].text;
valink.href = vlinks[0].text;
valink.innerText = vtitles[0].text;

tddesc.innerText = vdesc[0].text;
}
document.getElementById('title').appendChild(tmp);


}

No comments: