Home > Web Front-end > JS Tutorial > body text

JS reading xml file sample code under IE_javascript skills

WBOY
Release: 2016-05-16 17:26:45
Original
993 people have browsed it

Use JS to read xml files, only IE browser is considered here for now
step1 Create DOM object

Copy code The code is 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;
}

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:
Copy code The code is as follows:

var xmldom = document.implementation.createDocument(namespaceUri, root, doctype);

step2 Load xml file
Copy code The code is 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 The code is as follows:

var xmlDom = loadXML("config .xml");

Under IE, you can call the selectNodes() and selectSingleNode() methods to quickly locate nodes using XPath
Related labels:
ie
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!