AJAX 學習筆記之AJAX 對伺服器返回的XML的處理

來源:互聯網
上載者:User

假設伺服器返回的XML 文檔,如下所示:

 代碼如下 複製代碼

<?xml version="1.0" encoding="gb2312"?>
<list>
    <caption>Member List</caption>
    <member>
        <name>isaac</name>
        <class>W13</class>
        <birth>Jun 24th</birth>
        <constell>Cancer</constell>
        <mobile>1118159</mobile>
    </member>
    <member>
        <name>fresheggs</name>
        <class>W610</class>
        <birth>Nov 5th</birth>
        <constell>Scorpio</constell>
        <mobile>1038818</mobile>
    </member>
    <member>
        <name>girlwing</name>
        <class>W210</class>
        <birth>Sep 16th</birth>
        <constell>Virgo</constell>
        <mobile>1307994</mobile>
    </member>
    <member>
        <name>tastestory</name>
        <class>W15</class>
        <birth>Nov 29th</birth>
        <constell>Sagittarius</constell>
        <mobile>1095245</mobile>
    </member>
    <member>
        <name>lovehate</name>
        <class>W47</class>
        <birth>Sep 5th</birth>
        <constell>Virgo</constell>
        <mobile>6098017</mobile>
    </member>
    <member>
        <name>slepox</name>
        <class>W19</class>
        <birth>Nov 18th</birth>
        <constell>Scorpio</constell>
        <mobile>0658635</mobile>
    </member>
    <member>
        <name>smartlau</name>
        <class>W19</class>
        <birth>Dec 30th</birth>
        <constell>Capricorn</constell>
        <mobile>0006621</mobile>
    </member>
    <member>
        <name>tuonene</name>
        <class>W210</class>
        <birth>Nov 26th</birth>
        <constell>Sagittarius</constell>
        <mobile>0091704</mobile>
    </member>
    <member>
        <name>dovecho</name>
        <class>W19</class>
        <birth>Dec 9th</birth>
        <constell>Sagittarius</constell>
        <mobile>1892013</mobile>
    </member>
    <member>
        <name>shanghen</name>
        <class>W42</class>
        <birth>May 24th</birth>
        <constell>Gemini</constell>
        <mobile>1544254</mobile>
    </member>
    <member>
        <name>venessawj</name>
        <class>W45</class>
        <birth>Apr 1st</birth>
        <constell>Aries</constell>
        <mobile>1523753</mobile>
    </member>
    <member>
        <name>lightyear</name>
        <class>W311</class>
        <birth>Mar 23th</birth>
        <constell>Aries</constell>
        <mobile>1002908</mobile>
    </member>
</list>
 

 

用戶端獲得伺服器端的該XML 資料,並將其顯示在表格中。代碼如下:

 代碼如下 複製代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>responseXML</title>
<style>
<!--
.datalist{
    border:1px solid #744011;    /* 表格邊框 */
    font-family:Arial;
    border-collapse:collapse;    /* 邊框重疊 */
    background-color:#ffd2aa;    /* 表格背景色 */
    font-size:14px;
}
.datalist th{
    border:1px solid #744011;    /* 行名稱邊框 */
    background-color:#a16128;    /* 行名稱背景色 */
    color:#FFFFFF;                /* 行名稱顏色 */
    font-weight:bold;
    padding-top:4px; padding-bottom:4px;
    padding-left:12px; padding-right:12px;
    text-align:center;
}
.datalist td{
    border:1px solid #744011;    /* 儲存格邊框 */
    text-align:left;
    padding-top:4px; padding-bottom:4px;
    padding-left:10px; padding-right:10px;
}
.datalist tr:hover, .datalist tr.altrow{
    background-color:#dca06b;    /* 動態變色 */
}
input{    /* 按鈕的樣式 */
    border:1px solid #744011;
    color:#744011;
}
-->
</style>
<script language="javascript">
var xmlHttp;
function createXMLHttpRequest(){
    if(window.ActiveXObject)
        xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
    else if(window.XMLHttpRequest)
        xmlHttp = new XMLHttpRequest();
}
function getXML(addressXML){
    var url = addressXML + "?timestamp=" + new Date();
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET",url);
    xmlHttp.send(null);
}
function addTableRow(sName, sClass, sBirth, sConstell, sMobile){
    //表格添加一行的相關操作,可參看7.2.1節
    var oTable = document.getElementById("member");
    var oTr = oTable.insertRow(oTable.rows.length);    
    var aText = new Array();
    aText[0] = document.createTextNode(sName);
    aText[1] = document.createTextNode(sClass);
    aText[2] = document.createTextNode(sBirth);
    aText[3] = document.createTextNode(sConstell);
    aText[4] = document.createTextNode(sMobile);
    for(var i=0;i<aText.length;i++){
        var oTd = oTr.insertCell(i);
        oTd.appendChild(aText[i]);
    }
}
function DrawTable(myXML){
    //用DOM方法操作XML文檔
    var oMembers = myXML.getElementsByTagName("member");
    var oMember = "", sName = "", sClass = "", sBirth = "", sConstell = "", sMobile = "";
    for(var i=0;i<oMembers.length;i++){
        oMember = oMembers[i];
        sName = oMember.getElementsByTagName("name")[0].firstChild.nodeValue;
        sClass = oMember.getElementsByTagName("class")[0].firstChild.nodeValue;
        sBirth = oMember.getElementsByTagName("birth")[0].firstChild.nodeValue;
        sConstell = oMember.getElementsByTagName("constell")[0].firstChild.nodeValue;
        sMobile = oMember.getElementsByTagName("mobile")[0].firstChild.nodeValue;
        //添加一行
        addTableRow(sName, sClass, sBirth, sConstell, sMobile);
    }
}
function handleStateChange(){    
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
        DrawTable(xmlHttp.responseXML);    //responseXML擷取到XML文檔
}
</script>
</head>
 
<body>
<input type="button" value="擷取XML" onclick="getXML('9-4.xml');"><br><br>
<table class="datalist" summary="list of members in EE Studay" id="member">
    <tr>
        <th scope="col">Name</th>
        <th scope="col">Class</th>
        <th scope="col">Birthday</th>
        <th scope="col">Constellation</th>
        <th scope="col">Mobile</th>
    </tr>
</table>
</body>
</html>

 

我們可以看到,在用戶端獲得XML 檔案的代碼如下:

 代碼如下 複製代碼

<input type="button" value="擷取XML" onclick="getXML('9-4.xml');">

也就是說,是直接取得XML 資料的。而實際開發中返回XML 資料的工作是通過伺服器端(如:ASP.NET、JSP等)的代碼動態產生的。換句話說,getXML('...') 中的檔案地址應該是.aspx 或.jsp等動態網頁面的尾碼。

 

使用jQuery 架構實現

如果在用戶端使用jQuery 架構,實現AJAX 獲得伺服器端的XML資料。

代碼如下:

 代碼如下 複製代碼
<html>
<head>
  <title> demo </title>
  <meta name="Author" content="xugang" />
  <script language="javascript" src="jquery.min.js"></script>
  <script type="text/javascript">
  function getXML(addressXML){
    //使用jquery的ajax方法
    $.ajax({
       type:"GET",
       url:addressXML,
       dataType:"xml", //傳回型別(區分大小寫)
       success:function(myXML){
         //each 遍曆每個<member>標記
         $(myXML).find("member").each(
            function(){
              var oMember="",sName="",sClass="",sBirth="",sConstell="",sMobile="";
              sName = $(this).find("name").text();
              sClass = $(this).find("class").text();
              sBirth = $(this).find("birth").text();
              sConstell = $(this).find("constell").text();
              sMobile = $(this).find("mobile").text();
              //添加行
              $("#member").append($("<tr><td>"+sName
                 +"</td><td>" + sClass
                 +"</td><td>" + sBirth
                 +"</td><td>" + sConstell
                 +"</td><td>" + sMobile +"</td></tr>"));
            }
         )
       }
    })
  }
  </script>
</head>
<body>
  <input type="button" value="擷取XML" onclick="getXML('9-4.xml');">
  <br/>
  <TABLE class="datalist" id="member">
  <TR>
    <TH scope="col">Name</TH>
    <TH scope="col">Class</TH>
    <TH scope="col">Birthday</TH>
    <TH scope="col">Constellation</TH>
    <TH scope="col">Mobile</TH>
  </TR>
  </TABLE>
</body>
</html>

伺服器端傳遞XML 資料的方式不變

相關文章

聯繫我們

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