js解析xml浏览器兼容性处理

  1 /******************************************************************************
  2     说明:xml解析类
  3 ******************************************************************************/
  4
  5
  6 function XMLDOCDocument() {
  7     this.xmlDoc = null;
  8     this.async = false;
  9     this.xml = null;
 10 };
 11
 12
 13 function createDocument() {
 14     if (typeof arguments.callee.activeXString != "string") {
 15         var versions = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument"],
 16         i, len;
 17         for (i = 0, len = versions.length; i < len; i++) {
 18             try {
 19                 new ActiveXObject(versions[i]);
 20                 arguments.callee.activeXString = versions[i];
 21                 ActiveXObject(arguments.callee.activeXString);
 22                 break;
 23             } catch (ex) {
 24                 //跳过
 25             }
 26         }
 27     }
 28     return new ActiveXObject(arguments.callee.activeXString);
 29 };
 30 /*
 31 描述:加载xml
 32 */
 33 XMLDOCDocument.prototype.loadXML = function (xmlInfo) {
 34     //判断浏览器的类型
 35     //支持IE浏览器
 36
 37     var isLoad = false;
 38     var xmlDomVersions = [‘MSXML.2.DOMDocument.6.0‘, ‘MSXML.2.DOMDocument.3.0‘, ‘Microsoft.XMLDOM‘];
 39     for (var i = 0; i < xmlDomVersions.length; i++) {
 40         try {
 41             this.xmlDoc = new ActiveXObject(xmlDomVersions[i]);
 42             this.xmlDoc.async = this.async;
 43             this.xmlDoc.loadXML(xmlInfo); //loadXML方法载入xml字符串
 44             this.xml = this.xmlDoc.documentElement.outerHTML;
 45             isLoad = true;
 46             break;
 47         } catch (e) {
 48         }
 49     }
 50     if (!isLoad) {
 51         if (typeof DOMParser != "undefined") {
 52             this.xmlDoc = (new DOMParser()).parseFromString(xmlInfo, "text/xml");
 53             this.xml = this.xmlDoc.documentElement.outerHTML;
 54             var errors = this.xmlDoc.getElementsByTagName("parsererror");
 55             if (errors.length) {
 56                 throw new Error("XML parsing error:" + errors[0].textContent);
 57             }
 58         } else {
 59             throw new Error("No XML parser available.");
 60         }
 61     }
 62 };
 63
 64 /*
 65 说明:选择所有符合条件的节点
 66 */
 67 XMLDOCDocument.prototype.selectNodes = function (xPath) {
 68     return this.xmlDoc.selectNodes(xPath);
 69 };
 70 /*
 71 说明:选择一个符合条件的节点
 72 */
 73 XMLDOCDocument.prototype.selectSingleNode = function (xPath) {
 74     return this.xmlDoc.selectSingleNode(xPath);
 75 };
 76
 77
 78 /*
 79 说明:创建新节点
 80 */
 81 XMLDOCDocument.prototype.createElement = function (nodeName) {
 82     return this.xmlDoc.createElement(nodeName);
 83 };
 84
 85 /*
 86 说明:创建新属性
 87 */
 88 XMLDOCDocument.prototype.createAttribute = function (attrName) {
 89     return this.xmlDoc.createAttribute(attrName);
 90 };
 91 /*
 92 说明:获得xml字符串
 93 */
 94 XMLDOCDocument.prototype.getXml = function () {
 95     //return this.xmlDoc.xml;
 96     if (typeof this.xmlDoc.xml != "undefined") {
 97
 98         return this.xmlDoc.xml;
 99     }
100     else if (typeof XMLSerializer != "undefined") {
101         return (new XMLSerializer()).serializeToString(this.xmlDoc);
102     } else {
103         throw new Error("Could not serialize XML DOM.");
104     }
105 };
106 /*
107 说明:获得xml字符串
108 */
109 XMLDOCDocument.prototype.toString = function () {
110     //return this.xmlDoc.xml;
111     if (typeof this.xmlDoc.xml != "undefined") {
112
113         return this.xmlDoc.xml;
114     }
115     else if (typeof XMLSerializer != "undefined") {
116         return (new XMLSerializer()).serializeToString(this.xmlDoc);
117     } else {
118         throw new Error("Could not serialize XML DOM.");
119     }
120 };
121
122
123
124
125 // check for XPath implementation
126 if (document.implementation.hasFeature("XPath", "3.0")) {
127     // prototying the XMLDocument
128     XMLDocument.prototype.selectNodes = function (cXPathString, xNode) {
129         if (!xNode) {
130             xNode = this;
131         }
132         var oNSResolver = this.createNSResolver(this.documentElement)
133         var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
134         var aResult = [];
135         for (var i = 0; i < aItems.snapshotLength; i++) {
136             aResult[i] = aItems.snapshotItem(i);
137         }
138         return aResult;
139     };
140
141     // prototying the Element
142     Element.prototype.selectNodes = function (cXPathString) {
143         if (this.ownerDocument.selectNodes) {
144             return this.ownerDocument.selectNodes(cXPathString, this);
145         }
146         else {
147             throw "For XML Elements Only";
148         }
149     };
150
151     // prototying the XMLDocument
152     XMLDocument.prototype.selectSingleNode = function (cXPathString, xNode) {
153         if (!xNode) { xNode = this; }
154         var xItems = this.selectNodes(cXPathString, xNode);
155         if (xItems.length > 0) {
156             return xItems[0];
157         }
158         else {
159             return null;
160         }
161     };
162
163     // prototying the Element
164     Element.prototype.selectSingleNode = function (cXPathString) {
165         if (this.ownerDocument.selectSingleNode) {
166             return this.ownerDocument.selectSingleNode(cXPathString, this);
167         }
168         else { throw "For XML Elements Only"; }
169     }
170
171     Element.prototype.__defineGetter__("innerText",
172         function () {
173             return this.textContent;
174         }
175     );
176
177     Element.prototype.__defineSetter__("innerText",
178         function (sText) {
179             this.textContent = sText;
180         }
181     );
182
183 };
时间: 2024-11-05 23:36:39

js解析xml浏览器兼容性处理的相关文章

JS解析XML文件和XML字符串

JS解析XML文件 <script type='text/javascript'>    loadXML = function(xmlFile){        var xmlDoc=null;        //判断浏览器的类型        //支持IE浏览器        if(!window.DOMParser && window.ActiveXObject){            var xmlDomVersions = ['MSXML.2.DOMDocument.

js解析xml字符串或xml文件,将其转换为xml对象

注:判断是否是ie浏览器和非ie浏览器的方法有多种,在此只介绍用例中的方法: 1.解析xml字符串,得到xml对象的方式: function createXml(str){ if(document.all){//IE浏览器     var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");        xmlDoc.async = false;     xmlDoc.loadXML(str);     return xmlDoc; } el

CSS控制XML与通过js解析xml然后通过html显示xml中的数据

使用CSS控制XML的显示 book.css bookname{ display:block;color:Red} author{ display:block;font-style:italic} price{ display:block;colo book.xml <?xml version="1.0" encoding="GB2312" ?> <?xml-stylesheet type="text/css" href=&qu

JS解析XML实例

<script type="javascript"> var txt="<note>"; txt=txt+"<to>George</to>"; txt=txt+"<from>John</from>"; txt=txt+"<heading>Reminder</heading>"; txt=txt+"<

js解析xml,获取XMl标签属性值

<script type="text/javascript"> var xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +"<RecordInfo camId=\"1000000$1$0$0\" count=\"12\" />"; $(function(){ //加载xmlDoc v

JS解析xml字符串,并把xml展示在HTML页面上

首先,要写一个方法,把xml字符串转化成dom对象 //将字符串转化成dom对象;string转换为xml function stringToXml(xmlString) { var xmlDoc; if (typeof xmlString == "string") { //FF if (document.implementation.createDocument) { var parser = new DOMParser(); xmlDoc = parser.parseFromStr

js解析xml

var doc=xmlHttp.responseXML; //查询文档下名为student的所有元素,得到数据 var eleArr=doc.getElementsByTagName("student")[0]; var number=eleArr.getAttribute("number"); /* var number=eleArr.getAttribute("number"); var name=eleArr.getElementsByTa

js实现的解析xml文件和xml字符串代码实例

js实现的解析xml文件代码实例:下面分享一段代码实例,它实现了对xml文件的解析作用.代码如下: loadXML = function(xmlFile){ var xmlDoc=null; //判断浏览器的类型 //支持IE浏览器 if(!window.DOMParser && window.ActiveXObject){ var xmlDomVersions = ['MSXML.2.DOMDocument.6.0','MSXML.2.DOMDocument.3.0','Microsof

解析xml文件

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js 解析xml文件</title> </head> <body> <script type="text/javascript"> var xmlDoc=document.implementation.