• XML学习(1)


    什么是XML?

    XML是可拓展标记语言,类似HTML,它的设计宗旨是为了传输数据,而不是像HTML那样显示数据。XML标签没有被预定义,需要用户自定义标签。

    xml文档必须包含根元素,它是其他所有元素的父元素,比如这样的结构:

    <root>
      <child>
        <subchild>.....</subchild>
      </child>
    </root>

    root就是根元素

    实体

    在xml中不要使用字符"<"、">"….应该用实体引用来替代

    &lt; <
    $gt; >
    &amp; &
    &apos;   '
    &quot;   "

    在 XML 中,只有字符 "<" 和 "&" 确实是非法的。大于号是合法的,但是用实体引用来代替它是一个好习惯。

    注释:和html一样,都是<!--  …   -->

    命名的禁忌:

    避免使用" –  "字符,比如"a-b";避免使用 "   :   ",比如" a.b ";避免使用" :  "

    XMLHttpRequest对象用于在后台与服务器交换数据

    有了XMLHttpRequest,用户能:

    • 在不重新加载页面的情况下更新网页
    • 在页面已加载后从服务器请求数据
    • 在页面已加载后从服务器接收数据
    • 在后台向服务器发送数据

    创建方法:

    xmlhttp=new XMLHttpRequest();

    较为简单的代码示例:

    function createXmlHttpRequest(){    

        if(window.ActiveXObject){ //如果是IE

            return new ActiveXObject("Microsoft.XMLHTTP");    

        }else if(window.XMLHttpRequest){ //非IE

            return new XMLHttpRequest();    

        }    

    }    

    较为复杂的代码示例:

    function  createXmlHttpRequest(){    

      if(typeof  XMLHttpRequest!=”underfined”) {

        return  new  XMLHttpRequest();

    }     //for  IE7+  Firefox opera  chrome  safari

    else if(typeof  ActiveXObject!=”underfined”) {      //for IE6-

       if(typeof  arguments.callee.activeXString != “string”) {

         var  version =  ["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXMLHttp"], i  ,len;

        for ( i=0,len=version.length;i<len;i++) {

             try {

                     new  ActiveXobject(versions[i]);

                    arguments.callee.activeXString=version[i];

                      break;      

                    } catch(ex)       {

                         alert("please  update your IE");

                            }

                       }

                  }

                }

                  return arguments.callee.activeXString;

               }  else  {

                           throw  new  Error("XHR对象不可用");

                    }

    }

    创建完接口方法之后创建对象:

    var  xmlhttp=createXmlHttpRequest();

    状态判定:

    function  callback(){

      switch(xmlhttp.readyState)  {

            case 0:

                alert("请求未初始化");  break;

            case  1:

                alert("请求启动但是尚未发送");  break;

             case 2:

                alert("请求发送,尚未得到响应");  break;

              case 3:

                alert("请求开始响应,收到部分数据");  break;

               case 4:

                alert("请求响应完成,收到全部数据");          //交互完成

               if((xmlhttp.status>=200  &&  xmlhttp.status <300) || xml.status==304)

               {

                   var  data=xmlhttp.responseText;

               }  else  {

                 alert(“request  was successfully”+xmlhttp.status+xmlhttp.statusText);

                }

                 break;

          }

    };

    GET请求:

    xmlhttp=createXmlHttpRequest();

    var  url="getsomething.jsp?id" +id;

    xmlhttp.open("GET",url,true);    //选项为“true”,异步处理

    xmlhttp.onreadystatechange=callback;     //回调函数

    xmlHttp.setRequestHeader("Content-Type",

            "application/x-www-form-urlencoded;");     //针对不支持FormData的浏览器的处理

    xmlHttp.send(); 

    POST请求:

       var url = "posturl";

        xmlHttp.open("POST", url, true);          //选项为“true”,异步处理

        xmlHttp.onreadystatechange = callback;  //回调函数

        xmlHttp.setRequestHeader("Content-Type",

        "application/x-www-form-urlencoded;");      //针对不支持FormData的浏览器的处理

        xmlHttp.send(xmlfile); 

    或者:

       var  data={ID: "123",name:"jack"};

        xmlHttp.open("POST", "filename", true);          //选项为“true”,异步处理

       xmlHttp.onreadystatechange = callback;  //回调函数

      if(typeof FormData == "underfined") {

        xmlHttp.setRequestHeader("Content-Type", 

        "application/x-www-form-urlencoded;");      //针对不支持FormData的浏览器的处理

    }

        xmlHttp.send(post(data)); 

    open函数中的True 表示脚本会在 send() 方法之后继续执行,而不等待来自服务器的响应。如果设置为 "false",可以省去额外的 onreadystatechange 代码。如果在请求失败时是否执行其余的代码无关紧要,那么可以使用这个参数。

    在没有得到服务器响应的情况下,防止代码停止的最安全的方法就是设置onreadystatechange事件

    Technorati Tags:

    参考:https://www.cnblogs.com/yuanke/p/4998516.html

  • 相关阅读:
    sqlalchemy 基本操作
    Codeforces 716A Crazy Computer
    Codeforces 719B Anatoly and Cockroaches
    POJ 1067 取石子游戏
    P1028 数的计算
    P1914 一串字母
    P1308 统计单词数
    P1200 你的飞碟在这儿
    P1055 书号
    P1567 气温统计
  • 原文地址:https://www.cnblogs.com/simonid/p/6440699.html
Copyright © 2020-2023  润新知