However, the XML string is different in both browsers, IE can use the Loadxml method to parse XML strings directly, while under FF the Domparser object's Parsefromstring () method is used
var oparser=new domparser ();
Xmldoc=oparser.parsefromstring (Xmlstr, "text/xml");
In order to be able to use in two kinds of browsers, I thought of JAVASCRITP's unusual handling way, is try...catch ...
Copy Code code as follows:
function ToXML (strxml) {
try{
xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
Xmldoc.loadxml (strxml);
}
catch (e) {
var oparser=new domparser ();
Xmldoc=oparser.parsefromstring (Strxml, "text/xml");
}
return xmldoc;
}
<script type= "Text/javascript" > Function ToXML (strxml) {try{var xmldoc = new ActiveXObject ("Microsoft.X Mldom "); var xmldoc = new ActiveXObject ("Msxml2.domdocument"); Xmldoc.loadxml (strxml); catch (e) {var oparser=new domparser (); Xmldoc=oparser.parsefromstring (Strxml, "text/xml"); return xmldoc; } str= "<node1><node1_1>1400</node1_1><node1_2 id= ' n1_2 ' >default.htm</node1_2></ Node1> "var xmlobj=toxml (str); alert (Xmlobj.childnodes[0].firstchild.firstchild.nodevalue); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
See, whether in IE or FF all pop up 1400!!
Copy Code code as follows:
function Formattoxml (strxml) {
var Isie = function () {
var IE =/msie/i.test (navigator.useragent);
return IE;
}
var exc = function () {
var xmldoc = null;
if (Isie ())
{
xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
Xmldoc.loadxml (strxml);
}else{
FireFox2.0, Safari2.0
xmldoc = (new Domparser ()). Parsefromstring (Strxml, "text/xml");
}
return xmldoc;
}
return exc ();
}
function is the same, all in order to deal with the parsing of XML strings in IE and ff. However, in the worry-free some people say that XML strings should have <?xml version= "1.0" encoding= "gb2312", but if I have the xml version= "1.0" after the test Encoding= "gb2312", the parsing results are inconsistent, so I did not add these characters to the above example.