firefox解析xml文档的问题,大家帮忙啊!
一个解析xml文档,生成下拉框的js代码,在firefox下有点错误,实在找不到解决办法,大家帮帮我吧!^_^
代码如下:
var xmlDoc = null;
if (window.ActiveXObject)
{
xmlDoc =new ActiveXObject( "Microsoft.XMLDOM ");
xmlDoc .async=false;
xmlDoc .load( "/StaticData/Province.xml ");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc =document.implementation.createDocument( " ", " ",null);
xmlDoc .load( "/StaticData/Province.xml ");
}
else
{
alert( 'Your browser cannot handle this script ');
}
alert( xmlDoc );
alert( alert( xmlDoc );
.documentElement.getElementsByTagName( "NodeName ").length );
第一个 是xmldocument对象
第二个什么也没有 ,提示错误
错误: xmlDoc .documentElement has no properties
请文在firefox下怎么解析xml文件?
------解决方案-------------------- //重构loadXML,解决FireFox等浏览器没有loadXML方法.兼容处理
Document.prototype.loadXML = function(strXML) {
var objDOMParser = new DOMParser();
var objDoc = objDOMParser.parseFromString(strXML, "text/xml ");
while (this.hasChildNodes()){this.removeChild(this.lastChild);}
for (var i=0; i < objDoc.childNodes.length; i++) {
var objImportedNode = this.importNode(objDoc.childNodes[i], true);
this.appendChild(objImportedNode);
}
this.onload();
}//End Funciton
------解决方案--------------------加载顺序问题,最后的节点用nodeValue
<script>
var xmlDoc = null;
if (window.ActiveXObject)
{
xmlDoc =new ActiveXObject( "Microsoft.XMLDOM ");
xmlDoc.async=false;
xmlDoc.load( "./1.xml ");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc =document.implementation.createDocument( " ", "cd ",null);
xmlDoc.load( "./1.xml ");
}
else
{
alert( 'Your browser cannot handle this script ');
}
xmlDoc.onload=function(){
alert( xmlDoc );
alert(xmlDoc.documentElement.getElementsByTagName( "NodeName ")[0].childNodes[0].nodeValue );
}
</script>
------解决方案--------------------学习了,直接用js操作xml很容易出错,而且也即使不出错也不能保证在别的机器上不出错,因为msxml版本不一样,我一直回避直接这样做改用ajax+json代替了,懒惰了一点,但很可靠.
------解决方案--------------------下面是我以前写的,在FF和IE下都可以工作,你参考下
function createXMLHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP ");
}
}
function getDisAnimal0()
{
var url = "/products/xml/disease_animal.xml ";
var newsItemsDiv = " <ul> "
var lianimal = 0;
if(xmlHttp == null)
{
createXMLHttpRequest();
}
xmlHttp.open( "GET ", url, true);
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4 &&