Home > Backend Development > XML/RSS Tutorial > Detailed introduction on how to create DOM and load XML in IE

Detailed introduction on how to create DOM and load XML in IE

黄舟
Release: 2017-03-30 14:07:54
Original
1477 people have browsed it

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");
Copy after login

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);
Copy after login

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>");
Copy after login

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(&quot;Microsoft.XMLDOM&quot;);     //创建Microsoft解析器实例
xmldoc.async = false;
xmldoc.load("27_1.xml");  //载入指定的XML文档
var rootElement = xmldoc.documentElement;  //访问元素根节点
document.write(rootElement.nodeName);
</script>

Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template