JS讀取XML檔案資料並以table顯示資料(相容IEFirefox)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   使用   ar   

先看xml檔案:

<?xml version="1.0" standalone="yes"?><student>  <stuinfo>    <stuName>張秋麗</stuName>    <stuSex>女   </stuSex>    <stuAge>18</stuAge>  </stuinfo>  <stuinfo>    <stuName>李文才</stuName>    <stuSex>男   </stuSex>    <stuAge>31</stuAge>  </stuinfo>  <stuinfo>    <stuName>李斯文</stuName>    <stuSex>男   </stuSex>    <stuAge>22</stuAge>  </stuinfo>  <stuinfo>    <stuName>馬英</stuName>    <stuSex>女   </stuSex>    <stuAge>25</stuAge>  </stuinfo>  <stuinfo>    <stuName>孫紅雷</stuName>    <stuSex>男   </stuSex>    <stuAge>32</stuAge>  </stuinfo>  <stuinfo>    <stuName>歐陽俊雄</stuName>    <stuSex>男   </stuSex>    <stuAge>28</stuAge>  </stuinfo>  <stuinfo>    <stuName>江琳</stuName>    <stuSex>女   </stuSex>    <stuAge>23</stuAge>  </stuinfo>  <stuinfo>    <stuName>小小</stuName>    <stuSex>女   </stuSex>    <stuAge>22</stuAge>  </stuinfo></student>
View Code

aspx頁面代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="擷取資料庫資料產生XML.aspx.cs" Inherits="Chapter1.擷取資料庫資料產生XML" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>    <script type="text/javascript">        function loadXMLDoc(dname) {            if (window.XMLHttpRequest) {                xhttp = new XMLHttpRequest();            }            else {                xhttp = new ActiveXObject("Microsoft.XMLHTTP");            }            xhttp.open("GET", dname, false);            xhttp.send("");            return xhttp.responseXML;        }        function ReadXml() {            var xmldoc = loadXMLDoc("Student.xml");            //獲得指定節點            var divmsg = document.getElementById("xmlMsg");            var msg = "<table border=‘1‘ id=‘mytable‘><tr><th>姓名</th><th>性別</th><th>年齡</th><tr>";            var nodes = xmldoc.getElementsByTagName("stuinfo");            for (var i = 0; i < nodes.length; i++) {                msg += "<tr>";                msg += "<td>" + nodes[i].getElementsByTagName("stuName")[0].firstChild.nodeValue + "</td>";                msg += "<td>" + nodes[i].getElementsByTagName("stuSex")[0].firstChild.nodeValue + "</td>";                msg += "<td>" + nodes[i].getElementsByTagName("stuAge")[0].firstChild.nodeValue + "</td>";                msg += "</tr>";            }            msg += "</table>";            divmsg.innerHTML = msg;        }    </script></head><body>    <form id="form1" runat="server">    <div>        <input type="button" value="JS讀取XML" onclick="ReadXml()" /><br />        <div id="xmlMsg">        </div>    </div>    </form></body></html>

上面的JS操作主要就避免了使用childNodes(因為Firefox中childNodes[0]擷取到的是"\n"而不是我們想要的第一個子節點,這個自己可以去試下,反正我是遇到了這種情況),使得可以相容IE、Firefox,其他瀏覽器我沒試。

JS讀取XML檔案資料並以table顯示資料(相容IEFirefox)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.