Use Ajax technology to parse XML files

Source: Internet
Author: User

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! ");
}
}

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.