本文介绍了检索XML文件JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在W3Schools上找到了这个代码并对其进行了改进,最终它变得像这样: < pre lang = xml>< script language = javascript> function LoadXML(){ if ( window .XMLHttpRequest) { // IE7 +,Firefox,Chrome的代码, Opera,Safari xmlhttp = new XMLHttpRequest(); } 其他 { // IE6代码,IE5 xmlhttp = 新 ActiveXObject( Microsoft.XMLHTTP); } 尝试 { xmlhttp.open( GET, file.xml, false ); alert( first place); xmlhttp.send(); alert( 第二名); } catch (错误){ alert(err.message); } xmlDoc = xmlhttp.responseXML; document .write( <表>< TR><的第i;艺术家< /第><的第i;名称< /第>< / TR>中); var x = xmlDoc.getElementsByTagName( CD); for (i = 0 ; i< x.length; i ++) { document .write( < TR>< TD>中); alert( document .write(x [i] .getElementsByTagName( ARTIST)[ 0 ]。childNodes [ 0 ]。的nodeValue)); document .write( < / TD>< TD>中); alert( document .write(x [i] .getElementsByTagName( TITLE)[ 0 ]。childNodes [ 0 ]。的nodeValue)); document .write( < / TD>< / TR>中); } document .write( < /表>); } < / script> 此代码可用于:[ W3School代码] 但是当我想要在我自己的电脑上运行此代码,在try和catch块中会出现错误! 它显示& quot;无法加载& quot; file.xml& quot; 。 当我搜索此错误时,此主题[ link ]是错误的原因,所以我似乎无法在我自己的PC中做类似的事情,因为它使用http并且需要服务器或者本地主机。 但是这段代码适用于我班级的项目,其中一个场景是(检索XML文件的元素并在我的HTML上显示其内容)页面)! 我相信他们会在自己的电脑上测试这个程序。 所以任何人都可以解释一下在PC上完成这个场景的方法吗?! 注意:目标浏览器是IE 8.0 在此先感谢解决方案 您好看到这些链接以获得您的答案,谢谢您的回复。 http://stackoverflow.com/questions/1199180/read-xml-file-using-javascript [ ^ ] http://stackoverflow.com/questions/10684145/how-to-retrieve-xml-data-from-javascript [ ^ ] I found this code on W3Schools and improve it , in the end it became sth like this :<pre lang="xml"><script language= "javascript"> function LoadXML(){ if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } try{ xmlhttp.open("GET","file.xml",false); alert("first place"); xmlhttp.send(); alert("2nd place"); } catch(err){ alert(err.message); } xmlDoc=xmlhttp.responseXML; document.write("<table><tr><th>Artist</th><th>Title</th></tr>"); var x=xmlDoc.getElementsByTagName("CD"); for (i=0;i<x.length;i++) { document.write("<tr><td>"); alert( document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue)); document.write("</td><td>"); alert( document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue)); document.write("</td></tr>"); } document.write("</table>"); } </script> This code is available in :[W3School Code]But when I want to run this code in my own PC , an error occur in try and catch block !It shows that &quot;Cannot load the &quot;file.xml&quot; .When I search for this error a topic in this [link] is the reason of error , So It seems that I can&#39;t do something like that in my Own PC cuz it uses http and need a server or a local host .But This code is for a project in my class and one of the scenarios is (Retrieve Element of An XML File and show its content on my HTML page) !I am sure that they will test this program on their own PCs .So can anyone explain me a way to accomplish this scenario in A PC ?!Note : The target browser is IE 8.0Thanks in advance 解决方案 hi see these link to get your answer, thanks in advanced.http://stackoverflow.com/questions/1199180/read-xml-file-using-javascript[^]http://stackoverflow.com/questions/10684145/how-to-retrieve-xml-data-from-javascript[^] 这篇关于检索XML文件JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 21:30