Ajax control XML

Source: Internet
Author: User

 

<Html> <Script type = "text/javascript">
Var xmlHttp;
Function createXMLHttpRequest ()
{
If (window. ActiveXObject)
{
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest)
{
XmlHttp = new XMLHttpRequest ();
}
}
Function startRequest ()
{
CreateXMLHttpRequest ();
Try
{
XmlHttp. onreadystatechange = handleStateChange;
XmlHttp. open ("GET", "data. xml", true );
XmlHttp. send (null );
}
Catch (exception)
{
Alert ("The resource you want to access does not exist! ");
}
}
Function handleStateChange ()
{
If (xmlHttp. readyState = 4)
{
If (xmlHttp. status = 200 | xmlHttp. status = 0)
{
// Retrieve the xml dom object
Var xmlDOM = xmlHttp. responseXML; // get the root of the XML document
// Var res = document. getElementById ("res ");
// Res.innerhtml#xmldom.doc umentElement;
Var root = xmlDOM.doc umentElement;
Try
{// Obtain the <info> result
Var info = root. childNodes; // The returned result is displayed.
For (var I = 0; I <info. length; I ++) // traverses the output
Alert ("responseXML's value:" + info [I]. text );
}
Catch (exception)
{}
}
}
}
</Script> <Div id = "res"> </div> </body> Data. xml <? Xml version = "1.0" encoding = "GB2312"?>
<Root>
Fff
<Info> hello world! </Info>
<Goods> hello1 </goods>
<Goods> hello2 </goods>
</Root>

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.