The XML reading solution for Javascript is compatible with multiple browsers.

Source: Internet
Author: User
try{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e){
try{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e) {alert(e.message)}
}
try{
xmlDoc.async=false;
xmlDoc.load("example.xml");
document.write("xmlDoc is loaded");
}
catch(e){
alert(e.message)
}

Most of the scripts that can be found on the Internet to read XML files using JS are similar to the above. Fortunately, there are comments in the code that are compatible with Firefox, opera, Safari, and IE. In fact, after my tests, this code does not support safari at all, chrome of the same kernel is not supported either.

An error is reported in Safari:value undefined (result of expression xmlDoc.load) is not object. In Chrome, an error is reported:Uncaught TypeError: Object #<a Document> has no method 'load'.

It is strange that no related entries have been found in the error content searched on the Internet. Even if there are one or two entries, there is only a solution to the problem. In fact, XMLHttpRequest is used to read XML to avoid usingxmlDoc.load()Solution. I don't know if this is the only solution, in short, I wrote a section based on this-this time, JavaScript can be used in IE, opera, firexfox, Safari/chrome to normally read XML data-the demo address is here. You can view the source code of the JS script.

In addition, I tried to format and display the read data in An XSLT file. However, the Firefox browser has encountered<![CDATA[ ]]>The tag content is interpreted as an HTML obstacle. Besides, the JS solution mentioned last time does not seem to work.<xsl:copy-of select="node()"/>To make up.

Post a web blog

Code Used in my case: jsreadxml

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.