2011-07-04 121 views
1

投入HTML頁面,我需要通過包含信息,但一次一個,並顯示在HTML頁面中的XML文件中讀取。它需要像在<div>以及<p>的形式內的HTML中一樣滾動顯示。有人能幫我解決這個問題嗎?讀取XML的內容和使用JavaScript

abc.xml

<?xml version="1.0" encoding="iso-8859-1" ?> 

<projects> 

<project> 
     <flag>1</flag> 
     <name>Project 1</name> 
     <descp> Short description. </descp> 
     <rating> 6 </rating> 
     <link> URL1 </link> 
     </project> 

<project> 
     <flag>1</flag> 
     <name>Project 2</name> 
     <descp> Short description. </descp> 
     <rating> 9 </rating> 
     <link> URL2 </link> 
     </project> 

<project> 
     <flag>1</flag> 
     <name>Project 3</name> 
     <descp> Short description. </descp> 
     <rating> 4 </rating> 
     <link> URL3 </link> 
     </project> 

<project> 
     <flag>1</flag> 
     <name>Project 4</name> 
     <descp> Short description. </descp> 
     <rating> 5 </rating> 
     <link> URL4 </link> 
     </project> 

</projects> 

的JavaScript:

<script type="text/javascript"> 
window.onload = timer; 

function timer() { 
    var xmlDoc=new ActiveXObject("MSXML.DOMDocument"); 
    xmlDoc.async="false"; 
    xmlDoc.load("./gsss_hp.xml"); 
    var projects = xmlDoc.documentElement; 
    var project = projects.childNodes(0); 
    var numPro = projects.childNodes.length; // to find number of projects entered in the file 

    for(var i=0; i<numPro; i++){ 
     for(var j=0; j<100000;j++){j+=18; j-=18; for(var z=0;z<5000;z++){}} /* do noting for nested loops */ 
     show(i); 
    } 
} 

function show(num) { 
    var xmlDoc=new ActiveXObject("MSXML.DOMDocument"); 
    xmlDoc.async="false"; 
    xmlDoc.load("./gsss_hp.xml"); 

    var projects = xmlDoc.documentElement; 
    var project = projects.childNodes(num); 
    var attr = project.childNodes.length; 

    for(var i=0;i<attr;i++){ 
     var txt = xmlDoc.getElementsByTagName("project")[0].childNodes[i].nodeValue; 
     txt = txt + "<br />"; 
     var idPos; 
     if(i==1) { idPos = "name"; } 
     else 
     if(i==2) { idPos = "descp"; } 
     else 
     if(i==3) { idPos = "rating"; } 
     else 
     if(i==4) { idPos = "link"; } 

     document.getElementById(idPos).innerHTML = txt; 
    } 
} 

</script> 
+0

http://www.google.com/search?q=news+scroller+xml – mplungjan

回答

1

雖然我將離開實際的編碼給你,一般結構我會用這樣的事情是:

  • 通過加載XML開始。
  • 加載XML時,將數據提取到常規JavaScript數組中,以便於處理。
  • 使用的setTimeout調用繪製功能以指定的時間間隔(http://www.w3schools.com/js/js_timing.asp)和設置(全局)變量要記住哪個索引中顯示的最後一個。
  • 繪圖功能顯示數據,並增加變量。如果==長度,重置計數。
+0

http://w3fools.com/ – mplungjan