JavaScript functions to parse XML strings _javascript tips

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

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.