Suppose we now want to read the following Info.xml file
<?XML version= "1.0" encoding= "gb2312"?><Root> <DataID= "1"> <name>Ceun</name> < Age>21st</ Age> </Data> <DataID= "2"> <name><! [Cdata[Zhang San]]></name> < Age>22</ Age> </Data> <DataID= "3"> <name>Jake</name> < Age>23</ Age> </Data> <DataID= "4"> <name>Hello</name> < Age>20</ Age> </Data> <DataID= "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></HEAD><BODY><SCRIPTLANGUAGE= "JavaScript"><!--//Native XML object. functionCreatexmldom () {if(window. ActiveXObject)varxmldoc=NewActiveXObject ("Microsoft.XMLDOM"); Else if(Document.implementation&&document.implementation.createDocument)varxmldoc=Document.implementation.createDocument ("","Doc",NULL); Xmldoc.async= false; //in order to and Firefox one, here can not be changed to false;Xmldoc.preservewhitespace=true; returnxmldoc;}//loads an XML file. varXmlDom=createxmldom (); Xmldom.load ("Info.xml");//Get root nodevarRoot=xmldom.documentelement;varData="";varnames=Root.getelementsbytagname ("name");varAges=Root.getelementsbytagname (" Age");varLen=names.length; for(varI=0; I<Len;i++) {Data+="Name:"; Data+=names[i].firstchild.nodevalue; Data+="Age:"; Data+=ages[i].firstchild.nodevalue; Data+=" ";} alert (data); -</SCRIPT></BODY></HTML>
JS Read XML file