道非道 非常道

勤思、谨言、慎行、厚积、薄发

统计

web

天圆

经济 政治 军事

键康

WebFX 的 Xml Extras 基本应用

     XML and XML HTTP request abstraction.
             转自:http://webfx.eae.net/dhtml/xmlextras/xmlextras.html。
xmlextras.js
  1 //<script>
  2 //////////////////
  3 // Helper Stuff //
  4 //////////////////
  5 
  6 // used to find the Automation server name
  7 function getDomDocumentPrefix() {
  8     if (getDomDocumentPrefix.prefix)
  9         return getDomDocumentPrefix.prefix;
 10 
 11     var prefixes = ["MSXML2""Microsoft""MSXML""MSXML3"];
 12     var o;
 13     for (var i = 0; i < prefixes.length; i++) {
 14         try {
 15             // try to create the objects
 16             o = new ActiveXObject(prefixes[i] + ".DomDocument");
 17             return getDomDocumentPrefix.prefix = prefixes[i];
 18         }
 19         catch (ex) {};
 20     }
 21 
 22     throw new Error("Could not find an installed XML parser");
 23 }
 24 
 25 function getXmlHttpPrefix() {
 26     if (getXmlHttpPrefix.prefix)
 27         return getXmlHttpPrefix.prefix;
 28 
 29     var prefixes = ["MSXML2""Microsoft""MSXML""MSXML3"];
 30     var o;
 31     for (var i = 0; i < prefixes.length; i++) {
 32         try {
 33             // try to create the objects
 34             o = new ActiveXObject(prefixes[i] + ".XmlHttp");
 35             return getXmlHttpPrefix.prefix = prefixes[i];
 36         }
 37         catch (ex) {};
 38     }
 39 
 40     throw new Error("Could not find an installed XML parser");
 41 }
 42 
 43 //////////////////////////
 44 // Start the Real stuff //
 45 //////////////////////////
 46 
 47 
 48 // XmlHttp factory
 49 function XmlHttp() {}
 50 
 51 XmlHttp.create = function () {
 52     try {
 53         if (window.XMLHttpRequest) {
 54             var req = new XMLHttpRequest();
 55 
 56             // some versions of Moz do not support the readyState property
 57             // and the onreadystate event so we patch it!
 58             if (req.readyState == null) {
 59                 req.readyState = 1;
 60                 req.addEventListener("load"function () {
 61                     req.readyState = 4;
 62                     if (typeof req.onreadystatechange == "function")
 63                         req.onreadystatechange();
 64                 }, false);
 65             }
 66 
 67             return req;
 68         }
 69         if (window.ActiveXObject) {
 70             return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
 71         }
 72     }
 73     catch (ex) {}
 74     // fell through
 75     throw new Error("Your browser does not support XmlHttp objects");
 76 };
 77 
 78 // XmlDocument factory
 79 function XmlDocument() {}
 80 
 81 XmlDocument.create = function () {
 82     try {
 83         // DOM2
 84         if (document.implementation && document.implementation.createDocument) {
 85             var doc = document.implementation.createDocument(""""null);
 86 
 87             // some versions of Moz do not support the readyState property
 88             // and the onreadystate event so we patch it!
 89             if (doc.readyState == null) {
 90                 doc.readyState = 1;
 91                 doc.addEventListener("load"function () {
 92                     doc.readyState = 4;
 93                     if (typeof doc.onreadystatechange == "function")
 94                         doc.onreadystatechange();
 95                 }, false);
 96             }
 97 
 98             return doc;
 99         }
100         if (window.ActiveXObject)
101             return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
102     }
103     catch (ex) {}
104     throw new Error("Your browser does not support XmlDocument objects");
105 };
106 
107 // Create the loadXML method and xml getter for Mozilla
108 if (window.DOMParser &&
109     window.XMLSerializer &&
110     window.Node && Node.prototype && Node.prototype.__defineGetter__) {
111 
112     // XMLDocument did not extend the Document interface in some versions
113     // of Mozilla. Extend both!
114     //XMLDocument.prototype.loadXML =
115     Document.prototype.loadXML = function (s) {
116 
117         // parse the string to a new doc
118         var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
119 
120         // remove all initial children
121         while (this.hasChildNodes())
122             this.removeChild(this.lastChild);
123 
124         // insert and import nodes
125         for (var i = 0; i < doc2.childNodes.length; i++) {
126             this.appendChild(this.importNode(doc2.childNodes[i], true));
127         }
128     };
129 
130 
131     /*
132      * xml getter
133      *
134      * This serializes the DOM tree to an XML String
135      *
136      * Usage: var sXml = oNode.xml
137      *
138      */
139     // XMLDocument did not extend the Document interface in some versions
140     // of Mozilla. Extend both!
141     /*
142     XMLDocument.prototype.__defineGetter__("xml", function () {
143         return (new XMLSerializer()).serializeToString(this);
144     });
145     */
146     Document.prototype.__defineGetter__("xml"function () {
147         return (new XMLSerializer()).serializeToString(this);
148     });
149 }

加载  xml 文件
    使用异步加载
    
 1 function loadAsync(sUri) {
 2    var xmlHttp = XmlHttp.create();
 3    var async = true;
 4    xmlHttp.open("GET", sUri, async);
 5    xmlHttp.onreadystatechange = function () {
 6       if (xmlHttp.readyState == 4)
 7          doSomething(xmlHttp.responseXML); // responseXML : XmlDocument
 8    }
 9    xmlHttp.send(null);
10 }
11 
12 
     使用syncronous的 XMLHTTP
1 function loadSync(sUri) {
2    var xmlHttp = XmlHttp.create();
3    var async = false;
4    xmlHttp.open("GET", sUri, async);
5    xmlHttp.send(null);
6    doSomething(xmlHttp.responseXML); // responseXML : XmlDocument
7 }
8 
9 
     发送一个XML 文件
 1 function postAsync(sUri, xmlDoc) {
 2    var xmlHttp = XmlHttp.create();
 3    var async = true;
 4    xmlHttp.open("GET", sUri, async);
 5    xmlHttp.onreadystatechange = function () {
 6       if (xmlHttp.readyState == 4)
 7          doSomething(xmlHttp.responseXML); // responseXML : XmlDocument
 8    }
 9    xmlHttp.send(xmlDoc);
10 }
11 
      加载一个文本文件
1 function loadTextSync(sUri) {
2    var xmlHttp = XmlHttp.create();
3    var async = false;
4    xmlHttp.open("GET", sUri, async);
5    xmlHttp.send(null);
6    doSomething(xmlHttp.responseText); // responseText : String
7 }
8 
        使用XmlDocument
 1 var doc = XmlDocument.create();
 2 doc.loadXML( "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
 3    "<root>" +
 4       "<test name=\"Test 1\"/>" +
 5       "<test name=\"Test 2\"/>" +
 6    "</root>");
 7 
 8 var testEls = doc.getElementsByTagName("test");
 9 for (var i = 0; i < testEls.length; i++)
10    alert(tests[i].getAttribute("name"));
11 
      负载使用 XmlDocument
 1 function loadAsync2(sUri) {
 2    var doc = XmlDocument.create();
 3    doc.async = true;
 4    doc.onreadystatechange = function () {
 5       if (doc.readyState == 4)
 6          alert(doc.xml); // doc.xml : String
 7    }
 8    doc.load(sUri);
 9 }
10 

 我都有些迷惑,如果可以大家一起讨论一下。

posted on 2009-07-03 13:44 星期五 阅读(374) 评论(0)  编辑  收藏 所属分类: WEB FX


只有注册用户登录后才能发表评论。


网站导航: