JS操作XmlDocument 兼容操作 nsIDOMXPathEvaluator.createNS

分类:Javascript| 发布:camnprbubuol| 查看: | 发表时间:2010/12/29





javascript在读取xml文档,并解析时,存在浏览器不兼容现象,下边就来看一下会出现什么错误,以及解决兼容的问题。(重点兼容FF)


[Exception... "Not enough arguments [nsIDOMWindowInternal.alert]" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: {$->>repath}WebUi/3Linkage/Area.js :: anonymous :: line 93" data: no]


---------------------------------------------------------------------------------------


alert(this.xmlDoc.selectSingleNode("address").childNodes);

TypeError: this.xmlDoc.selectSingleNode is not a function


----------------------------------------------------------------------------------------


SelectSingleNode: function (xmlDoc, elementPath) { 
if (window.ActiveXObject) {
return xmlDoc.selectSingleNode(elementPath);
}
else {
var xpe = new XPathEvaluator();
var nsResolver = xpe.createNSResolver(xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);//报下边的错误
var results = xpe.evaluate(elementPath, xmlDoc, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return results.singleNodeValue;
}
}


[Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIDOMXPathEvaluator.createNSResolver]"  nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)"  location: "JS frame :: http://192.168.2.10/WebUi/3Linkage/Area.js :: anonymous :: line 80"  data: no]





if (!window.ActiveXObject) { 

Camnpr.ProvinceCityCountry.prototype.selectNodes = function (sXPath) {

var oEvaluator = new XPathEvaluator();

var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

var aNodes = new Array();

if (oResult != null) {

var oElement = oResult.iterateNext();

while (oElement) {

aNodes.push(oElement);

oElement = oResult.iterateNext();

}

}

return aNodes;

}

Camnpr.ProvinceCityCountry.prototype.selectSingleNode = function (sXPath) {

var oEvaluator = new XPathEvaluator();

// FIRST_ORDERED_NODE_TYPE returns the first match to the xpath.

var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

if (oResult != null) {

return oResult.singleNodeValue;

} else {

return null;

}

}

}


//判断浏览器类型
if (window.ActiveXObject) { //在IE浏览器环境下
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.load(XMLPath);
xmlNodes = xmlDoc.selectSingleNode("root").childNodes;

for (var i = 0; i < xmlNodes.length; i++) {
var provinceNode = xmlNodes[i].getAttribute("name");
var pOption = document.createElement("option");
pOption.value = provinceNode;
pOption.text = provinceNode;
provinceDropDownList.add(pOption);
}

InitCity();
}
else if (document.implementation && document.implementation.createDocument) { //在非IE浏览器环境下
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.load(XMLPath);
xmlDoc.async = false;

xmlDoc.onload = function () {
xmlNodes = xmlDoc.getElementsByTagName("province");

for (var i = 0; i < xmlNodes.length; i++) {
var provinceNode = xmlNodes[i].getAttribute("name");
var pOption = document.createElement("option");
pOption.innerHTML = provinceNode;
pOption.setAttribute("value", provinceNode);
provinceDropDownList.appendChild(pOption);
}

/* 初始化下级列表的过程必须放在
* XML文件的装载事件中,否则在初始化下
* 级列表的函数中无法获取上级列表的值
*/
InitCity();
}
}
365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/javascript/151.html