如下一段函数:
1 function createXmlDoc() {
2 var xmlProgid = new Array("MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0", "MSXML2.DOMDocument");
3 if (typeof(createXmlDoc._msxml) == "undefined") {
4 for (var i=0; i<xmlProgid.length; i++){
5 try {
6 var obj = new ActiveXObject(xmlProgid[i]);
7 createXmlDoc._msxml = i;
8 return obj;
9 } catch(oError) {}
10 }
11 } else {
12 return new ActiveXObject(xmlProgid[createXmlDoc._msxml]);
13 }
14 alert("MSXML does not installed in this machine.");
15 return null;
16 }
s
The code iterates through the "progIDs" array and instantiates the highest version MSXML DOM that is available on the machine and returns it to the caller (see below for details on which versions ship where). This has at least a few implications:
My goal for this post is to give a quick history of MSXML lifecycle and versions, provide details with an example on implementing best practices with MSXML on the web, and talk about a couple key things to watch out for.
If you want the full story please read on, but if you’re short on time and want the quick scoop here it is in 4 bullets:
MSXML Lifecycle & History
OK, the full story requires a little bit more context – so let’s cover the different versions of MSXML, where they ship, and what the long term strategy is.
Over the long run, the goal is to have our customers move their applications to MSXML6. In terms of deployment, we want to ship our technology "in-the-box" with the operating system so that page authors and app developers can take advantage of it with zero deployment. However, our customers have told us they want symmetrical XML APIs on all supported OS platforms, so we still need a way to get the newest XML technologies to our downlevel OSes (Win2k, Win XP, and Win 2k3).
MSXML6 will be part of the Vista operating system when it releases, but requires a redistributable package to be installed downlevel. We’d like to get MSXML6 “inlined” in the next service pack of each of the downlevel OSes, but we need a strong business case to do so. So in the short and medium term we will continue to ship a redistributable package for MSXML6 that can be installed on downlevel operating systems. We'll try to get a post up on the benefits of moving to MSXML6 sometime soon.
As much as we'd love everyone to be on MSXML6 today, we realize the migration can take some time. So we're continuing to invest in MSXML3 to support existing applications and applications that have zero deployment requirements. MSXML3 doesn't have all the improvements in MSXML6, but developers should consider it a robust and stable platform for MSXML applications. MSXML3 is already part of the operating system on a fully patched Win2k SP4 installation and higher so in general no deployment to the client is required. Going forward, MSXML3 updates will come out in each of the OS service packs. MSXML3 SP7 is the last update to MSXML3 that should ship in redistributable form and in the future no redistributable package should be necessary for our partners and customers to use MSXML3 functionality.
Finally, anyone using MSXML5 who isn’t writing applications specifically targeted at Microsoft Office 2003 or Microsoft Office 2007 should migrate to MSXML6.
The details
Once you pick a version of MSXML to use, how do you do it effectively? MSXML ships side-by-side with version dependent ProgIDs. That means two things:
var xmlDOM = new ActiveXObject('Msxml2.DOMDocument.3.0') //uses MSXML 3.0
var xmlDOM = new ActiveXObject('Msxml2.DOMDocument.6.0') //uses MSXML 6.0
One related note - service packs of a particular version of MSXML are not side by side and will upgrade that version of MSXML to the service pack version. For example, if your computer is running MSXML3 SP5 and you install MSXML3 SP7, all applications that use MSXML3 will automatically be upgraded to run on 3 SP7.
Ideally, customers should standardize on MSXML6, but as mentioned above legacy applications or zero-deployment requirements may block full migration to MSXML6 in the short run. In this case there are two tensions that need to be balanced – functionality and test costs. This essentially leads to two options:
if (Web.Application.get_type() == Web.ApplicationType.InternetExplorer) {
var progIDs = [ 'Msxml2.DOMDocument.6.0', 'Msxml2.DOMDocument.3.0'];
for (var i = 0; i < progIDs.length; i++) {
try {
var xmlDOM = new ActiveXObject(progIDs[i]);
return xmlDOM;
}
catch (ex) {
}
}
return null;
}
A couple things to watch out for
var xmlDOM = new ActiveXObject('Msxml2.DOMDocument.3.0')
and
var xmlDOM = new ActiveXObject('Msxml2.DOMDocument')