weidagang2046的專欄

          物格而后知致
          隨筆 - 8, 文章 - 409, 評論 - 101, 引用 - 0
          數據加載中……

          用Ajax讀取XML格式的數據

          用Ajax讀取XML格式的數據,只需要讀取XMLHttpRequest對象返回的responseXML屬性即可。代碼如下:

            1、Client - helloworld.htm

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
          >
          <html>
          <head>
          <title>Ajax Hello World</title>
          <script type="text/javascript">
          var xmlHttp;

          function createXMLHttpRequest(){
          if(window.ActiveXObject){
          xmlHttp
          =?new ActiveXObject("Microsoft.XMLHTTP");
          }
          else?if(window.XMLHttpRequest){
          xmlHttp
          =?new XMLHttpRequest();
          }
          }

          function startRequest(){
          createXMLHttpRequest();
          try{
          xmlHttp.onreadystatechange
          = handleStateChange;
          xmlHttp.open(
          "GET", "data.xml", true);
          xmlHttp.send(
          null);
          }
          catch(exception){
          alert(
          "您要訪問的資源不存在!");
          }
          }

          function handleStateChange(){
          if(xmlHttp.readyState ==?4){
          if (xmlHttp.status ==?200?|| xmlHttp.status ==?0){
          // 取得XML的DOM對象
          var xmlDOM = xmlHttp.responseXML;
          // 取得XML文檔的根
          var root = xmlDOM.documentElement;
          try
          {
          // 取得<info>結果
          var info = root.getElementsByTagName('info');
          // 顯示返回結果
          alert("responseXML's value: "?+ info[0].firstChild.data);
          }
          catch(exception)
          {

          }
          }
          }
          }
          </script>
          </head>
          <body>
          <div>
          <input type="button" value="return ajax responseXML's value"
          onclick
          ="startRequest();"?/>
          </div>
          </body>
          </html>

          2、Server - data.xml

          <?xml version="1.0" encoding="GB2312" ?>
          <root>
          <info>hello world!</info>
          </root>

          from: http://www.chinahtml.com/programming/8/2006/11624391147871.shtml

          posted on 2006-12-06 23:52 weidagang2046 閱讀(357) 評論(0)  編輯  收藏 所屬分類: Javascript

          主站蜘蛛池模板: 丹阳市| 施秉县| 大邑县| 广元市| 株洲市| 独山县| 元氏县| 始兴县| 宕昌县| 清河县| 冀州市| 大田县| 闽侯县| 始兴县| 汶上县| 恩平市| 松滋市| 宁波市| 天津市| 鲜城| 孟连| 和硕县| 延边| 来宾市| 仪征市| 隆林| 光泽县| 南汇区| 普兰县| 方城县| 长治县| 武冈市| 玛曲县| 友谊县| 邵武市| 津市市| 来宾市| 洪雅县| 龙里县| 通江县| 日土县|