js解析xml浏览器兼容性处理
/******************************************************************************
说明:xml解析类
******************************************************************************/ function XMLDOCDocument() {
this.xmlDoc = null;
this.async = false;
this.xml = null;
}; function createDocument() {
if (typeof arguments.callee.activeXString != "string") {
var versions = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument"],
i, len;
for (i = 0, len = versions.length; i < len; i++) {
try {
new ActiveXObject(versions[i]);
arguments.callee.activeXString = versions[i];
ActiveXObject(arguments.callee.activeXString);
break;
} catch (ex) {
//跳过
}
}
}
return new ActiveXObject(arguments.callee.activeXString);
};
/*
描述:加载xml
*/
XMLDOCDocument.prototype.loadXML = function (xmlInfo) {
//判断浏览器的类型
//支持IE浏览器 var isLoad = false;
var xmlDomVersions = ['MSXML.2.DOMDocument.6.0', 'MSXML.2.DOMDocument.3.0', 'Microsoft.XMLDOM'];
for (var i = 0; i < xmlDomVersions.length; i++) {
try {
this.xmlDoc = new ActiveXObject(xmlDomVersions[i]);
this.xmlDoc.async = this.async;
this.xmlDoc.loadXML(xmlInfo); //loadXML方法载入xml字符串
this.xml = this.xmlDoc.documentElement.outerHTML;
isLoad = true;
break;
} catch (e) {
}
}
if (!isLoad) {
if (typeof DOMParser != "undefined") {
this.xmlDoc = (new DOMParser()).parseFromString(xmlInfo, "text/xml");
this.xml = this.xmlDoc.documentElement.outerHTML;
var errors = this.xmlDoc.getElementsByTagName("parsererror");
if (errors.length) {
throw new Error("XML parsing error:" + errors[0].textContent);
}
} else {
throw new Error("No XML parser available.");
}
}
}; /*
说明:选择所有符合条件的节点
*/
XMLDOCDocument.prototype.selectNodes = function (xPath) {
return this.xmlDoc.selectNodes(xPath);
};
/*
说明:选择一个符合条件的节点
*/
XMLDOCDocument.prototype.selectSingleNode = function (xPath) {
return this.xmlDoc.selectSingleNode(xPath);
}; /*
说明:创建新节点
*/
XMLDOCDocument.prototype.createElement = function (nodeName) {
return this.xmlDoc.createElement(nodeName);
}; /*
说明:创建新属性
*/
XMLDOCDocument.prototype.createAttribute = function (attrName) {
return this.xmlDoc.createAttribute(attrName);
};
/*
说明:获得xml字符串
*/
XMLDOCDocument.prototype.getXml = function () {
//return this.xmlDoc.xml;
if (typeof this.xmlDoc.xml != "undefined") { return this.xmlDoc.xml;
}
else if (typeof XMLSerializer != "undefined") {
return (new XMLSerializer()).serializeToString(this.xmlDoc);
} else {
throw new Error("Could not serialize XML DOM.");
}
};
/*
说明:获得xml字符串
*/
XMLDOCDocument.prototype.toString = function () {
//return this.xmlDoc.xml;
if (typeof this.xmlDoc.xml != "undefined") { return this.xmlDoc.xml;
}
else if (typeof XMLSerializer != "undefined") {
return (new XMLSerializer()).serializeToString(this.xmlDoc);
} else {
throw new Error("Could not serialize XML DOM.");
}
}; // check for XPath implementation
if (document.implementation.hasFeature("XPath", "3.0")) {
// prototying the XMLDocument
XMLDocument.prototype.selectNodes = function (cXPathString, xNode) {
if (!xNode) {
xNode = this;
}
var oNSResolver = this.createNSResolver(this.documentElement)
var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
var aResult = [];
for (var i = 0; i < aItems.snapshotLength; i++) {
aResult[i] = aItems.snapshotItem(i);
}
return aResult;
}; // prototying the Element
Element.prototype.selectNodes = function (cXPathString) {
if (this.ownerDocument.selectNodes) {
return this.ownerDocument.selectNodes(cXPathString, this);
}
else {
throw "For XML Elements Only";
}
}; // prototying the XMLDocument
XMLDocument.prototype.selectSingleNode = function (cXPathString, xNode) {
if (!xNode) { xNode = this; }
var xItems = this.selectNodes(cXPathString, xNode);
if (xItems.length > 0) {
return xItems[0];
}
else {
return null;
}
}; // prototying the Element
Element.prototype.selectSingleNode = function (cXPathString) {
if (this.ownerDocument.selectSingleNode) {
return this.ownerDocument.selectSingleNode(cXPathString, this);
}
else { throw "For XML Elements Only"; }
} Element.prototype.__defineGetter__("innerText",
function () {
return this.textContent;
}
); Element.prototype.__defineSetter__("innerText",
function (sText) {
this.textContent = sText;
}
); };
js解析xml浏览器兼容性处理的更多相关文章
- js 解析XML 在Edge浏览器下面 无法准确读到节点属性值
js 解析XML 在Edge浏览器下面 无法准确读到节点属性值 Dom.documentElement.childNodes[j].attributes[2] 这个是大众写法 在win10的edge ...
- JS解析XML文件和XML字符串
JS解析XML文件 <script type='text/javascript'> loadXML = function(xmlFile){ var xmlDoc=null; //判断浏览 ...
- CSS控制XML与通过js解析xml然后通过html显示xml中的数据
使用CSS控制XML的显示 book.css bookname{ display:block;color:Red} author{ display:block;font-style:italic} p ...
- js解析xml,获取XMl标签属性值
<script type="text/javascript"> var xml="<?xml version=\"1.0\" enc ...
- node.js 解析xml BOM问题(xmlreader sax.js)
Email:longsu2010 at yeah dot net 之前写了两篇文章关于node.js解析xml,说的是xmlreader,文章如下 node.js解析xml(xmlreader) no ...
- 用js解析XML文件,字符串一些心得
解析XML文件遇到的问题 今天秦博士叫我解析一下XML文件,将里面的所有的X坐标Y坐标放在一个数组里面然后写在文档里让他进行算法比对,大家都知道了啦,解析XML文件获取里面的坐标数据什么的,当然是用前 ...
- JS 解析Xml
loadXML = function (xmlString) { var xmlDoc = null; //判断浏览器的类型 //支持IE浏览器 if (!window.DOMParser & ...
- tab.js分享及浏览器兼容性问题汇总
在 样式布局分享-基于frozen.js的移动OA 文章中,用了到第三方组件 tab.js(带菜单的横屏滑动插件),其兼容性很差,进行优化后,已兼容全平台(且支持IE6+). tab.js GitHu ...
- js解析XML
//在当前页面内追加换行标签和指定的HTML内容function w( html ){ $(document.body).append("<br/>" + htm ...
随机推荐
- Tomcat (安装及web实现 基础)
Tomcat服务器配置 安装:解压对应的版本就行 注意;不要把Tomcat放到有中文的和有空格的目录中 验证是否安装成功:进入安装盘的安装文件的bin目录下 执行startup bat 成功 80 ...
- eclipse设置author等注释
windows--> preference--> PyDev --> Editor --> Templates 点击New,新建一个template,输入name(之后选择这个 ...
- Android开发之漫漫长途 Ⅷ——Android Binder(也许是最容易理解的)
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- 中国十大B2C电商站点开发语言调查
中国B2C电商站点市场占有率排名例如以下 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I ...
- 我的GIS观
申明: 文章所述观点与不论什么组织或个人无关,仅代表我个人观点,如有不正确,还望批评指正. 概述: 从毕业到如今,在GIS这条路上也算是摸爬滚打4.5年了.说长也不长,说短也不短.在这4.5年的时间里 ...
- python中的subprocess.Popen()使用
python中的subprocess.Popen()使用 从python2.4版本开始,可以用subprocess这个模块来产生子进程,并连接到子进程的标准输入/输出/错误中去,还可以得到子进程的返回 ...
- HBase shell 命令介绍
HBase shell是HBase的一套命令行工具,类似传统数据中的sql概念,可以使用shell命令来查询HBase中数据的详细情况.安装完HBase之后,如果配置了HBase的环境变量,只要在sh ...
- 百度地图点集文档使用python的re模块处理成json的相关写法
这个实在不好起名字.写这个还不是因为被渣度坑的不要不要的.为什么说他坑呢.参考一下这两个截图的txt文档: 文档资源下载地址: http://lbsyun.baidu.com/index.php?t ...
- AsyncLocal的运作机制和陷阱
这是今天帮柠檬分析一个AsyncLocal相关的问题时发现的. 试想这个代码输出的值是多少? using System; using System.Threading; using System.Th ...
- gulp实现公共html代码复用
在开发网站的时候,尤其是类似于官网这样的项目,顶部都会有一个导航栏,底部会有一些其他信息,而这两个部分在每一个页面都是有的.我们不可能在每个html页面都写一遍,这样也不便后期维护等操作,所以可以把顶 ...