IE below JS read XML File sample code _javascript tips

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.