This is a javascriptProgram." Let's take it out today to learn and learn more! The three paragraphsCodeCommon Code.
VaR XMLHTTP;
// Create an XMLHTTPRequest object based on different browser types
Function createxmlhttprequest (){
If (window. activexobject ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else
If (window. XMLHttpRequest ){
XMLHTTP = new XMLHttpRequest ();
}
}
// Initiate an asynchronous request
Function startasyrequest (){
Createxmlhttprequest (); // call the createxmlhttprequest () XMLHTTPRequest object
XMLHTTP. onreadystatechange = handlestatechange;
XMLHTTP. Open ("get", "BBC. xml? Timespan = "+ new date (). gettime (), true );
XMLHTTP. Send (null );
}
// Processing function when the readystate value of the XMLHTTPRequest object changes
Function handlestatechange (){
If (XMLHTTP. readystate = 4 ){
If (XMLHTTP. Status = 200 ){
Bbcanalyze (); // call the function of parsing the XML file of the request
}
Else {
Alert ("Please check whether the Web server is running normally! ");
}
}
}
// Parse (using xml dom) the XML file requested and use HTML Dom to present data in table form
Function bbcanalyze (){
Xmldoc = XMLHTTP. responsexml;
VaR Table1 = Document. getelementbyid ("tb1 ");
// Alert (xmldoc );
If (xmldoc ){
VaR countrynodes = xmldoc. getelementsbytagname ("country ");
// Alert (countrynodes. Length );
For (VAR I = 0; I <countrynodes. length; I ++ ){
VaR T1 = Document. createelement ("TR ");
Var region = countrynodes [I]. getattribute ("region ");
VaR TD1 = Document. createelement ("TD ");
VaR newpnode1 = Document. createtextnode (region );
Td1.appendchild (newpnode1 );
T1.appendchild (TD1 );
VaR name = xmldoc. getelementsbytagname ("name") [I]. childnodes [0]. nodevalue;
VaR td2 = Document. createelement ("TD ");
VaR newpnode2 = Document. createtextnode (name );
Td2.appendchild (newpnode2 );
T1.appendchild (td2 );
VaR area = xmldoc. getelementsbytagname ("area") [I]. childnodes [0]. nodevalue;
VaR td3 = Document. createelement ("TD ");
VaR newpnode3 = Document. createtextnode (area );
Td3.appendchild (newpnode3 );
T1.appendchild (td3 );
VaR population = xmldoc. getelementsbytagname ("Population") [I]. childnodes [0]. nodevalue;
VaR td4 = Document. createelement ("TD ");
VaR newpnode4 = Document. createtextnode (population );
Td4.appendchild (newpnode4 );
T1.appendchild (td4 );
If (xmldoc. getelementsbytagname ("GDP") [I]. haschildnodes ()){
VaR GDP = xmldoc. getelementsbytagname ("GDP") [I]. childnodes [0]. nodevalue;
VaR newpnode5 = Document. createtextnode (GDP );
Td5 = Document. createelement ("TD ");
Td5.appendchild (newpnode5 );
T1.appendchild (td5 );
}
Else {
GDP = "";
Td6 = Document. createelement ("TD ");
VaR newpnode6 = Document. createtextnode (GDP );
Td6.appendchild (newpnode6 );
T1.appendchild (td6 );
}
Table1.appendchild (T1 );
// Alert (Table1 );
}
}
Else {
Alert ("The xmldoc file is empty. operation failed! ");
}
}