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.