How to load and read XML files using JS

Source: Internet
Author: User
Tags file url
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; h

2. 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!

Related Article

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.