DOM解析XML檔案

來源:互聯網
上載者:User

首先是定義一個XML檔案,檔案名稱為student.xml,代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<classmates>
  <student>
    <sid>05205020227</sid>
    <sname>YJ</sname>
    <sex>女</sex>
  </student>
  <student>
    <sid>05205020228</sid>
    <sname>YKF</sname>
    <sex>男</sex>
  </student>
  <student>
    <sid>05205020229</sid>
    <sname>ZDW</sname>
    <sex>男</sex>
  </student>
  <student>
    <sid>05205020230</sid>
    <sname>ZGJ</sname>
    <sex>男</sex>
  </student>
</classmates>

接下來就是一個網頁檔案,嵌入javascript指令碼代碼,如下:

<!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>
    <title>Dom解析HTML</title>
    <script language="javascript" type="text/javascript">
        var XMLHttpReq;
        var url = "student.xml";
        function createXMLHttpRequest()
        {
            if(window.XMLHttpRequest)
            {
                XMLHttpReq = new XMLHttpRequest(); //Mozilla瀏覽器
            }
            else if(window.ActiveXObject)
            {
                try
                {
                    XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e)
                {
                    try
                    {
                        XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch(e)
                    {
                       
                    }
                }
            }
        }
       
        //發送請求的函數
        function sendRequest()
        {
            createXMLHttpRequest();
            XMLHttpReq.onreadystatechange = processResponse;
            XMLHttpReq.open("GET",url,true);
            XMLHttpReq.send(null);
        }
       
        //處理響應的函數
        function processResponse()
        {
            if(XMLHttpReq.readyState==4)//判斷對象狀態
            {
                if(XMLHttpReq.status==200)//資訊已經成功返回,開始處理資訊
                {
                    readXml();
                }
                else
                {
                    //頁面不正常
                    window.alert(XMLHttpReq.statusText);
                    window.alert("請求的頁面有異常");
                }               
            }
        }
       
        //讀取XML文檔中的資料資訊的函數,既是解析函數
        function readXml()
        {
            var table = document.createElement("table");
            table.setAttribute("border","1");
            table.setAttribute("width","600");
            document.body.appendChild(table);
            var caption = "學生資訊"+url;
            table.createCaption().appendChild(document.createTextNode(caption));
            var header = table.createTHead();
            var headerrow = header.insertRow(0);
            headerrow.insertCell(0).appendChild(document.createTextNode("學號"));
            headerrow.insertCell(1).appendChild(document.createTextNode("姓名"));
            headerrow.insertCell(2).appendChild(document.createTextNode("性別"));
           
            var students = XMLHttpReq.responseXML.getElementsByTagName("student");
            for(var i=0;i<students.length;i++)
            {
                var stud = students[i];
                var sid = stud.getElementsByTagName("sid")[0].firstChild.data;
                var sname = stud.getElementsByTagName("sname")[0].firstChild.data;
                var sex = stud.getElementsByTagName("sex")[0].firstChild.data;
                var row = table.insertRow(i+1);
                row.insertCell(0).appendChild(document.createTextNode(sid));
                row.insertCell(1).appendChild(document.createTextNode(sname));
                row.insertCell(2).appendChild(document.createTextNode(sex));
            }
           
        }
    </script>
</head>
<body>
    <form >
        <input type="button" value="解析XML檔案"  onclick="sendRequest();"/>
    </form>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.