Function _ javascript skills for parsing xml strings using javascript

Source: Internet
Author: User
When operating XML files, we can use the Load method to directly Load the xml files, which is common in ie and ff. But it is an XML string, it will be different in the two browsers. in IE, you can directly use the LoadXML method to parse the XML string, and in FF, you need to use the parseFromString () of the DOMParser object () the method is
Var oParser = new DOMParser ();
XmlDoc = oParser. parseFromString (xmlStr, "text/xml ");
In order to be useful in both browsers, I think of the exception handling method of javascrui, that is, try... catch...

The Code is 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. XMLDOM "); // 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 select all Note: If you need to introduce external Js, You need to refresh it to execute]


Check that 1400 is displayed in both ie and ff !!

The Code is 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 and Safari2.0
XmlDoc = (new DOMParser (). parseFromString (strXml, "text/xml ");
}
Return XmlDoc;
}
Return Exc ();
}


The functions are the same. They are used to parse xml strings in ie and ff. However, some people say that xml strings must be If xml version = "1.0" encoding = "gb2312"?>, The parsing results are inconsistent, so the above example does not contain these characters.
Related Article

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.