JS operation XML instance Summary (loading and parsing XML files and strings) _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to operate XML in JavaScript, and summarizes and analyzes the related skills of loading and parsing XML files and strings in the form of examples, for more information about how to operate XML in JS, see the example in this article. We will share this with you for your reference. The details are as follows:

My xml file Login. xml is as follows.

<? Xml version = "1.0" encoding = "UTF-8"?>
  
   
    
    
    
    
    
   
   
    
    
   
   
   

Now I need to operate on the content of this xml file.

First, we need to load this xml file. xml files are loaded in js through XMLDOM.

// Load the xml document loadXML = function (xmlFile) {var xmlDoc; if (window. activeXObject) {xmlDoc = new ActiveXObject ('Microsoft. XMLDOM '); xmlDoc. async = false; xmlDoc. load (xmlFile);} else if (document. implementation & document. implementation. createDocument) {xmlDoc = document. implementation. createDocument ('','', null); xmlDoc. load (xmlFile);} else {return null;} return xmlDoc ;}

The xml file object comes out. Next, I will operate on this document.

For example, if we need to get the attributes of the first node of the node Login/Weapon/W, we can proceed as follows.

// First, judge the xml Object checkXMLDocObj = function (xmlFile) {var xmlDoc = loadXML (xmlFile); if (xmlDoc = null) {alert ('your browser does not support reading xml files, so this page prohibits your operations. We recommend you use IE5.0 or above to solve this problem! '); Window. location. href = '/Index. aspx ';} return xmlDoc;} // then start to get the value of var xmlDoc = checkXMLDocObj ('/EBS/XML/Login. xml'); var v = xmlDoc. getElementsByTagName ('login/Weapon/W') [0]. childNodes. getAttribute ('text ')

However, the way I write in my program is like this. Of course, the way I write in the program has been applied to the actual application.

InitializeSelect = function (oid, xPath) {var xmlDoc = checkXMLDocObj ('/EBS/XML/Login. xml'); var n; var l; var e = $ (oid); if (e! = Null) {n = xmlDoc. getElementsByTagName (xPath) [0]. childNodes; l = n. length; for (var I = 0; I
 
  

In the above access code, we use xmlDoc. getElementsByTagName (xPath.
You can also access it through xmldoc.doc umentElement. childNodes (1)... childNodes (0). getAttribute ('text.

Some common methods:

XmlDoc.doc umentElement. childNodes (0). nodeName, you can get the name of this node.
XmlDoc.doc umentElement. childNodes (0). nodeValue, you can get the value of this node. This value comes from the xml format like this: BSo we can get the value of B.
XmlDoc.doc umentElement. childNodes (0). hasChild, which can be used to determine whether a subnode exists.
In my experience, it is best to use the getElementsByTagName (xPath) method to access the node, because it can be used to locate the node directly through xPath, which will provide better performance.

Supplement:

Parse XML files using JS


 
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.