多浏览器载入XML方法

//将Mozilla 转化为IE模式 XmlDocUtil.js

 

 

var XmlDocUtil = new Object();

var sUserAgent = navigator.userAgent;
var fAppVersion = parseFloat(navigator.appVersion);
var isOpera = sUserAgent.indexOf("Opera")>-1;
var isKHTML = sUserAgent.indexOf("KHTML")>-1|| sUserAgent.indexOf("Konqueror")>-1 || sUserAgent.indexOf("AppleWebKit")>-1;
var isIE = sUserAgent.indexOf("conpatible")>-1 || sUserAgent.indexOf("MSIE")>-1 && !isOpera;
var isMoz = sUserAgent.indexOf("Gecko")>-1 && !isKHTML;

//var xmlDom = new xmlDom();

//create XmlDom
XmlDocUtil.XmlDom = function()
{
    if(window.ActiveXObject)
    {
        var arrSignatures = ["MSXML2.DOMDocument.5.0","MSXML2.DOMDocument.4.0",
                            "MSXML2.DOMDocument.3.0","MSXML2.DOMDocument","Microsoft.XmlDom"];
        for(var i =0; i <arrSignatures.length;i++)
        {
            try
            {
                var xmlDom = new ActiveXObject(arrSignatures[i]);
                return xmlDom;
            }
            catch(oError)
            {           
            }
        }
        throw new Error("MSXML is not installed on your system.");
    }
    else if(document.implementation && document.implementation.createDocument)
    {
        var xmlDom = document.implementation.createDocument("","",null);
        xmlDom.parseError =
        {
            valueOf: function() { return this.errorCode;},
            toString: function() { return this.errorCode.toString() }
        };
       
        xmlDom._initError_();
        xmlDom.addEventListener("load",function()
        {
            this._checkForErrors_();
            this._changeReadyState_(4);
        },false);
        return xmlDom;
    }
    else
    {
        throw new Error("Your browser doesn't support an XML DOM  object.");
    }
}

if(isMoz)
{
    Document.prototype.readyState = 0;
    Document.prototype.onreadystatechange= null;
    Document.prototype._changeReadyState_ = function(iReadyState)
    {
        this.readyState = iReadyState;
        if(typeof this.onreadystatechange == "function")
        {
            this.onreadystatechange();
        }
    };
   
    Document.prototype._initError_ = function()
    {
        this.parseError.errorCode = 0;
        this.parseError.filepos = -1;
        this.parseError.line = -1;
        this.parseError.linepos = -1;
        this.parseError.reason = null;
        this.parseError.srcText = null;
        this.parseError.url = null;
    };
   
    Document.prototype._checkForErrors_=function()
    {
        if(this.documentElement.tagName == "parsererror")
        {
            var reError = />([\s\S]*?)Location:([\s\S]*?)Line Number (\d+), Column (\d+):<sourcetext>([\s\S]*?)(?:\-*\^)/;
          
            reError.test(this.xml);
            this.parseError.errorCode = -999999;
            this.parseError.reason = RegExp.$1;
            this.parseError.url = RegExp.$2;
            this.parseError.line = parseInt(RegExp.$3);
            this.parseError.linepos = parseInt(RegExp.$4);
            this.parseError.srcText = RegExp.$5;           
        }
    };
   

    Document.prototype.loadXML = function(sXml)
    {
        this._initError_();
       
        this._changeReadeState_(1);
       
        var oParser = new DOMParser();
        var oXmlDom = oParser.parseFromString(sXml,"text/xml");
       
        while(this.firstChild)
        {
            this.removeChild(this.firstChild);
        }
       
        for(var i = 0;i < oXmlDom.childNodes.length;i++)
        {
            var oNewNode = this.importNode(oXmlDom.childNodes[i],true);
            this.appendChild(oNewNode);
        }
       
        this._checkForErrors_();
        this._changeReadyState_(4);
       
    };
   
    Document.prototype._load_ = Document.prototype.load;
   
    Document.prototype.load = function(sURL)
    {
        this._initError_();
        this._changeReadyState_(1);
        this._load_(sURL);
    };
   
    Node.prototype.__defineGetter__("xml",function()
    {
        var oSerializer = new XMLSerializer();
        return oSerializer.serializeToString(this,"text/xml");
    });   
 
}

//var oXmlDom = new XmlDocUtil.XmlDom();
//oXmlDom.onreadystatechange = function()
//{
//    if(oXmlDom.readyState == 4)
//    {
//        var oError = oXmlDom.parseError;
//        alert("An error occurred:\nError Code:"+oError.errorCode +"\n"
//                +"Line: " + oError.line+ "\n"
//                +"Line Pos: " +oError.linepos + "\n"
//                +"Reason: " +oError.reason);
//    }
//}

//oXmlDom.load("error.xml");

 

 

posted @ 2010-12-03 17:40  Vihone  阅读(412)  评论(0编辑  收藏  举报