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.