Another Data Transmission Mode in Ajax: XML

Source: Internet
Author: User

XML
Is an ubiquitous data format in modern computers. Ajax applications are in the Web browser environment, especially the XMLHTTPRequest object, which provides good local support for processing XML. If xzmlhttprequest receives an XML content type, for example, application/XML or test/XML, it will respond to a Dom!
See the script below:

<SCRIPT>
VaR offset = 8;
Function showpopup (name, description)
{

VaR win = new datapopup (name, description, offset, offset, 320,320 );
Offset + = 32;

}
Function datapopup (name, description, X, Y, W, H)
{
VaR BOD = Document. createelement ("Div ");
Document. Body. appendchild (BOD );
This. contentdiv = Document. createelement ("Div ");
This. contentdiv. classname = "windcontents ";
This. contentdiv. innerhtml = description;
BOD. appendchild (this. contentdiv );
This. Win = new windows. Window (BOD, name, X, Y, W, H );
}

Function showinfo (Event)
{
VaR planet = This. ID;
VaR scripturl = planet + ". xml ";
New net. contentloader (scripturl, parsexml );
}

Function parsexml ()
{
VaR name = "";
VaR descrip = "";
VaR xmldoc = This. Req. responsexml;
VaR eldocroot = xmldoc. getelementsbytagname ("planet") [0];
If (eldocroot)
{
Attrs = eldocroot. attributes;
Name = attrs. getnameditem ("name"). value;
VaR ptype = attrs. getnameditem ("type"). value;
If (ptype)
{
Descrip + = "<H2>" + ptype + "</H2> ";
}
Descrip + = "<ul> ";
For (VAR I = 0; I <eldocroot. childnodes. length; I ++)
{
Elchild = eldocroot. childnodes [I];
If (elchild. nodename = "info ")
{
Descrip + = "<li>" + elchild. firstchild. Data + "</LI> \ n ";
}
}
Descrip + = "</ul> ";
}
Else {
Alert ("no document ");
}
Top. showpopup (name, descrip );
}
</SCRIPT>

The showinfo function simply opens an XMLHTTPRequest object encapsulated in the contentloader object and provides the parsexml () method as the callback. This callback function is a little more troublesome than the evalscript () method, because we need to navigate to the response Dom, extract data from it, and then manually call the showpopup () method ,! This data can be used in XML-centered applications!
A major advantage of XML is its ability to structure information! In this script, parsexml () is used to convert the functions into HTML sdks!

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.