Use JS to read XML files, here only consider IE browser
Step1 Create a DOM object
Copy Code code as follows:
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;
}
IE8 Previously, XMLDOM was implemented using ActiveX objects, and after IE9, IE began to support Level 2 DOM (other browsers that support Level 2 DOM include Firefox, Opera, Chrome, and Safari)
You can create an XML DOM object as follows:
Copy Code code as follows:
var xmldom = Document.implementation.createDocument (NamespaceURI, Root, doctype);
step2 load XML file
Copy Code code as follows:
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:
Copy Code code as follows:
var xmldom = Loadxml ("config.xml");
IE, you can call the SelectNodes () and selectSingleNode () methods, using XPath to quickly locate nodes