Introduction
1. Create an instance of XML DOMobject
Microsoft The ActiveXObject class for creating ActiveX objects is introduced in JavaScript, through which instances of XML DOM objects can be created. The code is as follows:
var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
2, Loading XML
Microsoft's XML DOM has two methods of loading XML, namely load() and loadXML().
Method 1:
The load() method is used to load XML files from the server. The syntax format of the load() method is as follows:
xmldoc.load(url);
Parameter description:
xmldoc: is an instance of XML DOM object.
url: is the name of the XML file.
Note: The
load() method can only load files stored on the same server as the page containing JavaScript.
When loading, you can also use synchronous or asynchronous modes. By default, files are loaded in asynchronous mode. If you need to load synchronously, you can set the asyncproperty to false.
When loading a file asynchronously, you also need to use the readyState attribute and onreadystatechangeEvent processingfunction, so as to ensure that other operations are performed after the DOM is fully loaded.
Method 2: The
loadXML() method can directly input XMLstring to the XML DOM, for example:
xmldoc.loadXML("<root><son/></root>");
Second Get XML The root node application of the document
implements the integrated application of XML, DOM and JavaScript in the instance.
First use ActiveXObject to create a Microsoft parser instance, then load the XML document into memory, then use the DOM object to obtain the root node in the XML document (var rootElement = xmldoc.documentElement;), and finally output the root node .
Three codes
获取XML文档的根结点 <script> var xmldoc = new ActiveXObject("Microsoft.XMLDOM"); //创建Microsoft解析器实例 xmldoc.async = false; xmldoc.load("27_1.xml"); //载入指定的XML文档 var rootElement = xmldoc.documentElement; //访问元素根节点 document.write(rootElement.nodeName); </script>
Four running results
Software management system
The above is the detailed content of Detailed introduction on how to create DOM and load XML in IE. For more information, please follow other related articles on the PHP Chinese website!