JS操作XmlDocument 兼容操作 nsIDOMXPathEvaluator.createNS
分类:Javascript| 发布:camnprbubuol| 查看:1619 | 发表时间:2010/12/29
javascript在读取xml文档,并解析时,存在浏览器不兼容现象,下边就来看一下会出现什么错误,以及解决兼容的问题。(重点兼容FF)
1 | [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] |
---------------------------------------------------------------------------------------
1 | alert(this.xmlDoc.selectSingleNode("address").childNodes); |
3 | TypeError: this.xmlDoc.selectSingleNode is not a function |
----------------------------------------------------------------------------------------
01 | SelectSingleNode: function (xmlDoc, elementPath) { |
02 | if (window.ActiveXObject) { |
03 | return xmlDoc.selectSingleNode(elementPath); |
06 | var xpe = new XPathEvaluator(); |
07 | var nsResolver = xpe.createNSResolver(xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement); |
08 | var results = xpe.evaluate(elementPath, xmlDoc, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null ); |
09 | return results.singleNodeValue; |
1 | [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] |
01 | if (!window.ActiveXObject) { |
03 | Camnpr.ProvinceCityCountry.prototype.selectNodes = function (sXPath) { |
05 | var oEvaluator = new XPathEvaluator(); |
07 | var oResult = oEvaluator.evaluate(sXPath, this , null , XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); |
09 | var aNodes = new Array(); |
11 | if (oResult != null ) { |
13 | var oElement = oResult.iterateNext(); |
17 | aNodes.push(oElement); |
19 | oElement = oResult.iterateNext(); |
29 | Camnpr.ProvinceCityCountry.prototype.selectSingleNode = function (sXPath) { |
31 | var oEvaluator = new XPathEvaluator(); |
35 | var oResult = oEvaluator.evaluate(sXPath, this , null , XPathResult.FIRST_ORDERED_NODE_TYPE, null ); |
37 | if (oResult != null ) { |
39 | return oResult.singleNodeValue; |
02 | if (window.ActiveXObject) { |
03 | xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" ); |
04 | xmlDoc.async = "false" ; |
06 | xmlNodes = xmlDoc.selectSingleNode( "root" ).childNodes; |
08 | for ( var i = 0; i < xmlNodes.length; i++) { |
09 | var provinceNode = xmlNodes[i].getAttribute( "name" ); |
10 | var pOption = document.createElement( "option" ); |
11 | pOption.value = provinceNode; |
12 | pOption.text = provinceNode; |
13 | provinceDropDownList.add(pOption); |
18 | else if (document.implementation && document.implementation.createDocument) { |
19 | xmlDoc = document.implementation.createDocument( "" , "" , null ); |
23 | xmlDoc.onload = function () { |
24 | xmlNodes = xmlDoc.getElementsByTagName( "province" ); |
26 | for ( var i = 0; i < xmlNodes.length; i++) { |
27 | var provinceNode = xmlNodes[i].getAttribute( "name" ); |
28 | var pOption = document.createElement( "option" ); |
29 | pOption.innerHTML = provinceNode; |
30 | pOption.setAttribute( "value" , provinceNode); |
31 | provinceDropDownList.appendChild(pOption); |