Use JS to read xml files, only IE browser is considered here for now
step1 Create DOM object
function createDom() {
var xmlDoc = null;
try { //IE
if (typeof arguments.callee.activeXString != ' string') {
var versions = [
"MSXML2.DOMDocument.6.0",
"MSXML2.DOMDocument.3.0",
"MSXML2.DOMDocument",
"Microsoft.XMLDOM"
];
var i,
len;
for (i = 0, len = versions.length; i < len; i) {
try {
new ActiveXObject( versions[i]);
arguments.callee.activeXString = versions[i];
break;
} catch (ex) {
// ignore
}
}
}
xmlDoc = new ActiveXObject(arguments.callee.activeXString);
} catch (ex) { // other
xmlDoc = document.implementation.createDocument("", "", null);
}
return xmlDoc;
}
Before IE8, XmlDom was implemented using ActiveX objects. After IE9, IE began to support Level 2 DOM, (others support Level 2 DOM browsers include Firefox, Opera, Chrome, and Safari, etc.)
You can create XML DOM objects as follows:
var xmldom = document.implementation.createDocument(namespaceUri, root, doctype);
step2 Load xml file
function loadXML(file) {
var dom = createDom();
if (dom == null) {
alert("load filed!");
}
try {
dom.async = false;
dom .load(file);
} catch (ex) {
alert("unsupport browser!");
}
return dom;
}
A simple example:
var xmlDom = loadXML("config .xml");
Under IE, you can call the selectNodes() and selectSingleNode() methods to quickly locate nodes using XPath