| <html> <head> <title>Ajax read xml file</title> <script language="javascript"> var req = false; function doRequest(url) { req = false; if (window.XMLHttpRequest) { // non IE browsers req = new XMLHttpRequest(); if (req.overrideMimeType) { req.overrideMimeType('text/xml'); } } else if (window.ActiveXObject){ // IE try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!req) { alert('Error: Cannot create an XMLHTTP instance'); return false; } req.onreadystatechange = processXml; req.open('GET', url, true); req.send(null); } function processXml() { if (req.readyState == 4) { if (req.status == 200) { var xmldoc = req.responseXML; var cont=true; var i=0; var obj = document.getElementById("div_1"); var read_data="<br>"; while(cont) { root_node = xmldoc.getElementsByTagName('player').item(i); if(root_node == null) cont = false; else { read_data = read_data +"<li>"+ root_node.firstChild.data + "</li>";} i++; } obj.innerHTML += read_data; } else { alert('Error: null or invalid request.'); } } } </script> </head> <body text="#000080" bgcolor="#C0C0C0"> Click the link , and the content of the XML file will be displayed.<br> <a href="#" onclick="doRequest('player.xml')">Retreive data</a> <div width="200px" height="100px" backcolor="grey" id="div_1"><u>Content of xml file :</u> </div> </body> </html> |
| player.xml <?xml version="1.0" encoding="utf-8" ?> |
![]() |
![]() |