This article mainly introduces how to load and read XML files in javascript, and analyzes the implementation steps and operation skills related to loading and reading xml files in javascript in the form of examples, for more information about how to load and read XML files in JS, this article analyzes the implementation steps and Operation Skills of javascript for loading and reading xml files in the form of examples. For more information, see
This document describes how to load and read XML files in JavaScript. We will share this with you for your reference. The details are as follows:
When JS is used for loading and reading XML files during development, write down the file for reference. The following two steps are completed:
1. JS load XML files
The steps are generally (1) to create an xml dom object; (2) to set the loading method, asynchronous (recommended) or synchronous; (3) to provide the XML File URL and then call the load method; it is roughly as follows:
Var xmlFileName = "xxFile. xml "; var xmlDoc =''; if (window. activeXObject) {// IE var activeXNameList = new Array ("MSXML2.DOMDocument. 6.0 "," MSXML2.DOMDocument. 5.0 "," MSXML2.DOMDocument. 4.0 "," MSXML2.DOMDocument. 3.0 "," MSXML2.DOMDocument "," Microsoft. XMLDOM "," MSXML. DOMDocument "); for (var h = 0; h2. JS nodes for reading XML files
After loading the XML file is to read the XML file node, you can use the DOM corresponding method, the ms ie other browsers read similar, for example:
For example, the XML file structure below:
shenzhen
shenzhenNBA
man
shenzhen
xiaoming
woman
zhangsan
man
// JS reads the area node in the XML file as follows: var nodeList = xmlDoc.doc umentElement. getElementsByTagName ("area"); // IEfor (var I = 0; I
There are also some methods to read nodes:
// MS IEnode. text; // read the node's text value node. childNodes [I]. text; // read the text node of the I-th [direct next level] subnode under the node. getAttribute ("attributeName"); // read the attribute name of the node as the attribute value of attributeName. // other methods can be found online.
// Non-MS IEnode. nodeValue; // read the node's text value node. childNodes [I]. nodeValue; // read the text node of the I-th [direct next level] subnode under the node. getAttribute ("attributeName"); // read the attribute name of the node as the attribute value of attributeName. // other methods can be found online.
The above is a detailed description of the example for loading and reading XML files using JS. For more information, see other related articles in the first PHP community!