0% found this document useful (0 votes)
9 views3 pages

Assi 5

The document contains JavaScript code that retrieves data from JSON, XML, and TXT files and displays them in table format within a web browser. It includes functions to handle XMLHttpRequests for each file type and dynamically generates HTML tables to present the data. Additionally, there is a comparison function to check if the data from the three sources matches.

Uploaded by

muskansh7860
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Assi 5

The document contains JavaScript code that retrieves data from JSON, XML, and TXT files and displays them in table format within a web browser. It includes functions to handle XMLHttpRequests for each file type and dynamically generates HTML tables to present the data. Additionally, there is a comparison function to check if the data from the three sources matches.

Uploaded by

muskansh7860
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment-5

=> write javascript code to display txtfile data, xmlfile data, jsonfile data in table formate at browser.

<html>
<script>
var ar=new Array;
var ax=new Array;
var ac=new Array;
function getjson()
{
var r=new XMLHttpRequest();
r.open("GET","a.json");
r.onreadystatechange=function()
{
if(this.readyState==4 && this.status==200)
{
var res=this.responseText;

var x=JSON.parse(res);

var txt="<table border=10 bgcolor=blue><tr><th>rno</th><th>sname</th><tr>";


n=0;
for(k in x)
{
for(l in x[k])
{
ar[n]=x[k][l].rno;
n++;
txt+="<tr><td>"+x[k][l].rno+"</td><td>"+x[k][l].sname+"</td></tr>";
}
}
txt+="</table>";
document.getElementById("id1").innerHTML=txt;
}

}
r.send();
}

function getxml()
{
var r=new XMLHttpRequest();
r.open("GET","a.xml")
r.onreadystatechange=function()
{
if(this.readyState==4 && this.status==200)
{
var res=this.responseXML;
var x=res.getElementsByTagName("rollno");
var y=res.getElementsByTagName("sname");

var txt1="<table border=10 bgcolor=blue><tr><th>rno</th><th>sname</th><tr>";


c=0;
for(i=0;i<x.length;i++)
{
ax[c]=x[i].childNodes[0].nodeValue;
c++;

txt1+="<tr><td>"+x[i].childNodes[0].nodeValue+"</td><td>"+y[i].childNodes[0].nodeValue+"</
td></tr>";
}
txt1+="</table>";
}
document.getElementById("id2").innerHTML=txt1;
}
r.send();
}

function gettxt()
{
var req=new XMLHttpRequest();
req.open("GET","a.txt");
req.onreadystatechange=function()
{
if(this.readyState==4 && this.status==200)
{
var d=this.responseText;
var a="<table border=10 bgcolor=blue><tr><th>data txt</th></tr>";
x=0;
for(var i=0;i<d.length;i++)
{
var part=d.substr(i,1);
ac[x]=part;
x++;
a=a+"<tr><td>"+part+"</td></tr>";
}
a=a+"</table>";
document.getElementById("id3").innerHTML=a;
}

}
req.send();
}

function com()
{
var ans="<table border=10 bgcolor=blue><tr><th>rollno</th></tr>";
for(var i=0;i<ac.length;i++)
{
if(ax[i]==ar[i] && ax[i]==ac[i])
{
ans=ans+"<tr><td>"+ac[i]+"</td></tr>";
}
else
{
alert("data is not same");
}
}
ans=ans+"</table>";
document.getElementById("id4").innerHTML=ans;
}
</script>
<body style="background-color: rgb(197, 133, 183);">
<body>
<button onclick=getjson()>load json</button>
<button onclick=getxml()>load xml</button>
<button onclick=gettxt()>load txt </button>

<div id=id1>json data</div>


<div id=id2>xml data</div>
<div id=id3>txt data</div>
<div id=id4>compare data</div>
<button onclick=com()>compare</button>
</body>
</html> output:-

You might also like