JS Note Note (8)--js loading an XML string or file

Source: Internet
Author: User

1. Loading an XML file

method 1:ajax Way . The code is as follows:

var New New ActiveXObject ("Microsoft.XMLHTTP"); Xhr.open (false); Xhr.send (null); var xmldoc =

(For XMLHttpRequest object usage, please participate in http://www.w3school.com.cn/xmldom/dom_http.asp)

Note that the second line of code is "false", which means that it is not asynchronous. If this is changed to "true", then xmldoc will get null. Because the asynchronous operation of JS does not wait for the file to finish loading, it executes the following statement directly. So, we have to set this to "false", which means you have to wait for the file to finish loading and then do the following to get the correct xmldoc.

This approach is compatible with all advanced browsers and is recommended for loading in this way.

method 2:ie the way . The code is as follows:

var New ActiveXObject ("Microsoft.XMLDOM"= "false"; Xmldoc.load ("Note.xml");

The file is loaded via the load () method of the ActiveXObject ("Microsoft.XMLDOM") object that is unique to ie.

Note that the async is still set to false for the same reason as Method 1.

method 3:firefox, the code is as follows:

var NULL  = "false"; Xmldoc.load ("Note.xml"); Console.log (xmldoc) ;

about cross-domain loading : For security reasons, modern browsers cannot be accessed across domains-that is, only the XML files on this computer can be loaded.

2. Loading an XML string

Look at the code first:

1       functionLoadxmltext () {2 3             //Stitching XML Strings4             varTXT = ";5TXT = txt + "<note>";6TXT = txt + "<to>George</to>";7TXT = txt + "<from>John</from>";8TXT = txt + ";9TXT = txt + "<body>don ' t forget the meeting!</body>";TenTXT = txt + "</note>"; One  A              -             if(window. Domparser) { -                 //non-IE browser thexmldoc = (NewDomparser ()). parsefromstring (TXT, "Text/xml"); -}Else { -                 //IE Browser -xmldoc =NewActiveXObject ("Microsoft.XMLDOM");  +                 //or: xmldoc = new ActiveXObject ("MSXML2.      DOMDocument ");  -  +Xmldoc.async = "false";//do not enable async to ensure that the following actions are not performed until the file is loaded successfully A xmldoc.loadxml (TXT); at             } -  - Console.log (xmldoc); -}

If the browser supports window. Domparser object, the XML string is loaded directly with its parsefromstring () method.

IE browser does not support Window.domparser, it is loaded with Loadxml ().

The comments in the code are written in a very pro-you clear.

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.