2012-08-30 45 views
0

我想通過javascript讀取xml文件並將其顯示在HTML頁面中。使用javascript讀取本地XML文件並在html頁面中顯示

我正在使用以下代碼。

function readxml() { 
alert("calledreadxml"); 
var myXML, studentnode;  
myXML= document.all("config.xml").XMLDocumentalert(myXML); 
studentnode = myXML.documentElement 
var string; 
var nxtnode; 
while(nxtnode) 
{ 
    string = "the value of "+nxtnode.nodeName+"is :"+ nxtnode.nodeValue+"\n"; 
    nxtnode = myXML.nextSibling; 
} 
} 

這個代碼是NT工作..任何人都可以請幫助

+0

好吧,既然你不能用JavaScript讀取本地文件,你可能要在這裏是出於運氣。另外document.all是一種獲取DOM節點的IE方式,而不是讀取文件。什麼是XMLDocumentalert? – aquinas

+0

有一些[新東西](http://www.html5rocks.com/en/tutorials/file/dndfiles/),可以讓你這樣做。讀完文件後,請查看全局對象DOMParser和XMLSerializer。 – batzkoo

+0

請詳細說明「不能工作」。這是不足的信息。另外,我強烈建議使用像jQuery這樣的庫。噢,正如@aquinas所說,不要使用'document.all':它在大多數瀏覽器中都不起作用。 – Spudley

回答

-3
<script type="text/javascript"> 

          if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
           xmlhttp = new XMLHttpRequest(); 
          } 
          else {// code for IE6, IE5 
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
          } 
          xmlhttp.open("GET", "Images-Info.xml", false); 
          xmlhttp.send(); 
          xmlDoc = xmlhttp.responseXML; 


                var x = xmlDoc.getElementsByTagName("Image_Store"); 

          for (i = 0; i < x.length; i++) 
          { 
           var type = x[i].getElementsByTagName("Image_Type")[0].childNodes[0].nodeValue; 
           var title = x[i].getElementsByTagName("Image_Title")[0].childNodes[0].nodeValue; 
           var des = x[i].getElementsByTagName("Image_Description")[0].childNodes[0].nodeValue; 
           var ph = x[i].getElementsByTagName("Image_Photo")[0].childNodes[0].nodeValue; 
           var thm = x[i].getElementsByTagName("Thumbnail_Photo")[0].childNodes[0].nodeValue; 
           // Ul1.innerHTML = Ul1.appendChild("<li><div><a href=\"" + ph + "\" \><img src=\"" + thm + "\" \/></a><p>" + title + "</p></div><div class=\"data\"><div><h1>" + title + "</h1>" + des + "</div></div></li>"); 
           document.write("<li>"); 
           document.write("<div>"); 
           document.write("<a href=\"" + ph + "\" \>"); 
           document.write("<img src=\"" + thm + "\" \/>"); 
           document.write("</a>"); 
           document.write("<p>"); 
           document.write(title); 
           document.write("</p>"); 
           document.write("</div>"); 
           document.write("<div class=\"data\">"); 
           document.write("<div>"); 
           document.write("<h1>"); 
           document.write(title); 
           document.write("</h1>"); 
           document.write(des); 
           document.write("</div>"); 
           document.write("</div>"); 
           document.write("</li>"); 
          }     
     </script> 
+4

請格式化您的答案..這是荒謬的 – Rob

+0

這是嗎?沒有上下文... – anu

相關問題