The XML read method of JavaScript manipulating XML file _javascript tips

Source: Internet
Author: User

This article illustrates the XML read method of JavaScript operation XML file. Share to everyone for your reference. The specific analysis is as follows:

Let's say we're going to read the following Info.xml file

<?xml version= "1.0" encoding= "gb2312"?>
<root>
 <data id= "1" >
 <name>ceun</ name>
 <age>21</age>
 </data>
 <data id= "2" >
 <name><![ cdata[John]]></name>
 <age>22</age>
 </data>
 <data id= "3" >
 < name>jake</name>
 <age>23</age>
 </data>
 <data id= "4" >
 < name>hello</name>
 <age>20</age>
 </data>
 <data id= "5" >
 < name>paul</name>
 <age>25</age>
 </data>
</root>

Next, read and traverse the Info.xml

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <TITLE> Hello </title&
Gt
</HEAD> <BODY> <script language= "JavaScript" > <!--//Raw XML object. function Createxmldom () {if (window).
  ActiveXObject) var xmldoc=new activexobject ("Microsoft.XMLDOM"); else if (document.implementation&&document.implementation.createdocument) var Xmldoc=document.implementati
  On.createdocument ("", "Doc", NULL);
  Xmldoc.async = false;
  In order to and Firefox one to, this can not be changed to false;
  Xmldoc.preservewhitespace=true;
return xmldoc;
}//load XML file.
var xmldom=createxmldom ();
Xmldom.load ("Info.xml");
Obtain the root node var root=xmldom.documentelement;
var data= "";
var names=root.getelementsbytagname ("name");
var ages=root.getelementsbytagname ("Age");
var len=names.length;
 for (Var i=0;i<len;i++) {data+= "name:";
 Data+=names[i].firstchild.nodevalue;
 data+= "Age:";
 Data+=ages[i].firstchild.nodevalue;
data+= "";}
alert (data); --> </SCRIPT> </bOdy> </HTML> 

The

wants this article to help you with your JavaScript programming.

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.