2012-03-27 146 views
1

我試圖將一天中的一些電視節目表存儲到XML中,爲TV列表製作DTD驗證文件,然後製作一個純HTML網頁,將XML數據轉換爲表格。在HTML表格中顯示XML數據?

我敢肯定,問題在於我在做什麼與HTML或我的XML標籤的結構。我知道有一種Javascript方法可能比我在HTML中做的更好,但我明確要求這樣做。

HTML:http://pastebin.com/1LgujZUj

+0

你想解析XML作爲HTML表? – lvil 2012-03-27 12:30:47

+1

您似乎試圖使用XML數據島。別。它們是非標準功能。如果你想要一個XML數據源,那麼在將它傳遞給瀏覽器之前,使用*服務器端*程序將其轉換爲HTML。 – Quentin 2012-03-27 12:32:11

回答

2

您可以嘗試使用XSTL或像一個javascript腳本:

<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","cd_catalog.xml",false); 
xmlhttp.send(); 
xmlDoc=xmlhttp.responseXML; 

document.write("<table border='1'>"); 
var x=xmlDoc.getElementsByTagName("CD"); 
for (i=0;i<x.length;i++) 
{ 
    document.write("<tr><td>"); 
    document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); 
    document.write("</td><td>"); 
    document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); 
    document.write("</td></tr>"); 
} 
document.write("</table>"); 
</script> 

http://www.w3schools.com/xml/xml_to_html.asp

+0

儘管這在理論上可以回答這個問題,但[這將是更可取的](http://meta.stackexchange.com/q/8259)在這裏包含了答案的重要部分,並提供了供參考的鏈接。 – 2012-03-27 17:02:30