javascript - Link Xml file to HTML -
i have created simple xml file , html file display xml file details not working..its in same folder all...
here xml file :
student.xml
<?xml version="1.0" encoding="utf-8"?> <student> <name>student</name> <gender>boy</gender> <city>mumbai</city> <phonenumbers>42607700, 42607701</phonenumbers> <email>student@aurus.com</email> </student>
html file :
studentdetails.html
<!doctype html> <html> <head> <script src="loadxmldoc.js"></script> </head> <body> <script> xmldoc=loadxmldoc("student.xml"); x=xmldoc.getelementsbytagname("student"); (i=0;i<x.length;i++) { document.write(x[i].childnodes[0].nodevalue); document.write("<br>"); } </script> </body> </html>
and javascript file :
loadxmldoc.js
function loadxmldoc(student) { if (window.xmlhttprequest) { xhttp=new xmlhttprequest(); } else // code ie5 , ie6 { xhttp=new activexobject("microsoft.xmlhttp"); } xhttp.open("get",student,false); xhttp.send(); return xhttp.responsexml; }
plz help
i wrote example working in firefox not in chrome. think because requesting xml file locally. should through server.
<!doctype html> <html> <head> <style> table, th, td { border: 1px solid black; border-collapse:collapse; } th, td { padding: 5px; } </style> </head> <body> <script> if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.open("get","students.xml",false); xmlhttp.send(); xmldoc=xmlhttp.responsexml; document.write("<table><tr><th>students</th><th>age</th></tr>"); var x=xmldoc.getelementsbytagname("student"); (i=0;i<x.length;i++) { document.write("<tr><td>"); document.write(x[i].getelementsbytagname("name")[0].childnodes[0].nodevalue); document.write("</td><td>"); document.write(x[i].getelementsbytagname("age")[0].childnodes[0].nodevalue); document.write("</td></tr>"); } document.write("</table>"); </script> </body> </html>
students.xml
<?xml version="1.0" encoding="utf-8"?> <students> <student> <name>hanzallah</name> <age>20</age> <exp>everything</exp> </student> <student> <name>afgan</name> <age>22</age> <exp>nothing</exp> </student> </students>
Comments
Post a Comment