Sunday, August 22, 2010

How To Read Data From Xml File Using Ajax

**First Create your page as follows
<html xmlns="http://www.w3.org/1999/xhtml">


<head runat="server">

<title></title>

<script type="text/javascript">



<script type="text/javascript">

function loadXMLDoc() {

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp = new XMLHttpRequest();

}

else {// code for IE6, IE5

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

xmlhttp.onreadystatechange = function () {

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

xmlDoc = xmlhttp.responseXML;



var txt = "<table style=" + "width:600px;" + " cellpadding=" + "2px" + ">";

x = xmlDoc.getElementsByTagName("ID");

y = xmlDoc.getElementsByTagName("Name");

z = xmlDoc.getElementsByTagName("Roll");

for (i = 0; i < x.length; i++) {

txt = txt + "<tr style=" + "width:600px;" + ">";

txt = txt + "<td>";

txt = txt + x[i].childNodes[0].nodeValue + " " + "</td><td>";

txt = txt + y[i].childNodes[0].nodeValue + " " + "</td><td>";

txt = txt + z[i].childNodes[0].nodeValue + " " + "</td></tr>";

}

document.getElementById("myDiv").innerHTML = txt + "</table>";

}

}

xmlhttp.open("GET", "XMLFile1.xml", true);

xmlhttp.send();

}

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

<h2>

My CD Collection:</h2>

<div id="myDiv">

</div>

<button type="button" onclick="loadXMLDoc()">

Get my CD collection</button>

</div>

</form>

</body>

</html>
** Add and exit the following xml file page

<?xml version="1.0" encoding="utf-8" ?>


<studentInformation>

-<student>

<ID>1</ID>

<Name>jamal</Name>

<Roll>343</Roll>

</student>

-<student>

<ID>2</ID>

<Name>kamal</Name>

<Roll>343</Roll>

</student>

-<student>

<ID>3</ID>

<Name>Rabby</Name>

<Roll>343</Roll>

</student>

-<student>

<ID>4</ID>

<Name>Hasan</Name>

<Roll>34332</Roll>

</student>

</studentInformation>