首先给出跨浏览器的对document的创建,跨ie和firefox,
xmldom.js文件
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
function
XmlDom()
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (window.ActiveXObject)
{
var arrSignatures = ["MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0",
"MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument",
"Microsoft.XmlDom"];
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var i=0; i < arrSignatures.length; i++)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
var oXmlDom = new ActiveXObject(arrSignatures[i]);
return oXmlDom;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (oError)
{
//ignore
}
}
![](/Images/OutliningIndicators/InBlock.gif)
throw new Error("MSXML is not installed on your system.");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else if (document.implementation && document.implementation.createDocument)
{
var oXmlDom = document.implementation.createDocument("","",null);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
oXmlDom.parseError =
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
valueOf: function ()
{ return this.errorCode; },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
toString: function ()
{ return this.errorCode.toString() }
};
oXmlDom.__initError__();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
oXmlDom.addEventListener("load", function ()
{
this.__checkForErrors__();
this.__changeReadyState__(4);
}, false);
![](/Images/OutliningIndicators/InBlock.gif)
return oXmlDom;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else
{
throw new Error("Your browser doesn't support an XML DOM object.");
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
if
(isMoz)
{
![](/Images/OutliningIndicators/InBlock.gif)
Document.prototype.readyState = 0;
Document.prototype.onreadystatechange = null;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Document.prototype.__changeReadyState__ = function (iReadyState)
{
this.readyState = iReadyState;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (typeof this.onreadystatechange == "function")
{
this.onreadystatechange();
}
};
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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;
};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Document.prototype.__checkForErrors__ = function ()
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (this.documentElement.tagName == "parsererror")
{
![](/Images/OutliningIndicators/InBlock.gif)
var reError = />([\s\S]*?)Location:([\s\S]*?)Line Number (\d+), Column (\d+):<sourcetext>([\s\S]*?)(?:\-*\^)/;
![](/Images/OutliningIndicators/InBlock.gif)
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;
}
};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Document.prototype.loadXML = function (sXml)
{
this.__initError__();
this.__changeReadyState__(1);
var oParser = new DOMParser();
var oXmlDom = oParser.parseFromString(sXml, "text/xml");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while (this.firstChild)
{
this.removeChild(this.firstChild);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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);
![](/Images/OutliningIndicators/InBlock.gif)
};
Document.prototype.__load__ = Document.prototype.load;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Document.prototype.load = function (sURL)
{
this.__initError__();
this.__changeReadyState__(1);
this.__load__(sURL);
};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Node.prototype.__defineGetter__("xml", function ()
{
var oSerializer = new XMLSerializer();
return oSerializer.serializeToString(this, "text/xml");
});
![](/Images/OutliningIndicators/InBlock.gif)
}
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
![](/Images/OutliningIndicators/None.gif)
<
html
>
<
head
>
<
title
>
Cross-Browser XML DOM Example
</
title
>
<
script
type
="text/javascript"
src
="xmldom.js"
></
script
>
</
head
>
<
body
>
<
p
>
This example loads an invalid XML file and displays an error message in an alert.
</
p
>
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
script
type
="text/javascript"
>
var oXmlDom = new XmlDom();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
oXmlDom.onreadystatechange = function ()
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (oXmlDom.readyState == 4)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (oXmlDom.parseError != 0)
{
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);
![](/Images/OutliningIndicators/InBlock.gif)
}
}
};
oXmlDom.load("errors.xml");
![](/Images/OutliningIndicators/InBlock.gif)
</
script
>
</
body
>
</
html
>