weidagang2046的專欄

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

          用Ajax讀取XML格式的數(shù)據(jù)

          用Ajax讀取XML格式的數(shù)據(jù),只需要讀取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>結(jié)果
          var info = root.getElementsByTagName('info');
          // 顯示返回結(jié)果
          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

          主站蜘蛛池模板: 黑山县| 四平市| 崇义县| 龙里县| 汤原县| 英山县| 沁水县| 罗田县| 体育| 阿巴嘎旗| 湖南省| 和硕县| 玉龙| 镇远县| 宜丰县| 阿鲁科尔沁旗| 凤冈县| 巴南区| 抚州市| 吉林省| 张掖市| 丁青县| 龙游县| 西充县| 临颍县| 方山县| 遵义市| 兰溪市| 门头沟区| 黔西县| 西贡区| 威海市| 思茅市| 白银市| 洪泽县| 肥东县| 即墨市| 周宁县| 泗洪县| 海安县| 肥乡县|