Inspired? No home

Microsoft XML Parser sniffer

Unfortunately there are many versions of Microsoft XML Parser installed on clients computers. When using functions and methods of the parser it doesn’t really matter that much which version you use, but there are significant speed and stability improvements in newer versions. So you should always use the latest version if possible. Here is a code which sniffs the parser version:

UPDATE:
This code has been updated and also includes a new function which you can use to easily create a new XML Parser object and don’t worry about the version installed. Click here to see the updated version.


<pre name="code" class="javascript">
function XMLClientVer()
{
//*******
// Public properties
//
*******

this.bIsMSXML4 = false;
this.bIsMSXML3 = false;
this.bIsMSXML2 = false;

//************
// Private implementation details
//
************

var e = new Error();
var oXML = null;

// Try to load the most recent version of the MSXML parser;
// if that fails, try to load the next most recent version, and so on.
// Always test using the version dependent PROGID.

try
{
// Test for MSXML 4.0
oXML = new ActiveXObject(“MSXML2.DOMDocument.4.0”);
oXML = null;
this.bIsMSXML4 = true;
return;
}
catch (e)
{
try
{
// Test for MSXML 3.0
oXML = new ActiveXObject(“MSXML2.DOMDocument.3.0”);
oXML = null;
this.bIsMSXML3 = true;
return;
}
catch (e)
{
try
{
// Test for MSXML 2.0
oXML = new ActiveXObject(“Microsoft.XMLDOM.1.0”);
oXML = null;
this.bIsMSXML2 = true;
return;
}
catch (e)
{
// Stub
}
}
}
}
</pre>

put this code in your shared function’s javascript file, and then in your script where using the parser include:

<pre name="code" class="javascript">
// Create a new instance of the XMLClientVer object
var oXMLClientVer = new XMLClientVer();

if(oXMLClientVer.bIsMSXML4)
{
var objMSXMLParser = ‘Msxml2.DOMDocument.4.0’;
}
else if(oXMLClientVer.bIsMSXML3)
{
var objMSXMLParser = ‘Msxml2.DOMDocument.3.0’;
}
else if(oXMLClientVer.bIsMSXML2)
{
var objMSXMLParser = ‘Msxml2.DOMDocument’;
}
else
{
alert(‘MS XML PARSER NOT INSTALLED’);
}
</pre>

Written on 18 June 2004.
blog comments powered by Disqus