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