Javascript Ajax parsing XML is compatible with Firefox/IE

Source: Internet
Author: User
// Import the JS File

Function getresult (URL, ready ){
VaR XMLHTTP;
VaR r = function (){

If (XMLHTTP. readystate = 4 ){
If (XMLHTTP. Status = 200 ){
// Alert (isie );

VaR xmlstr;
VaR xmldoc;
VaR isie = !! (Window. attachevent &&! Window. Opera );
If (isie)
Xmldoc = XMLHTTP. responsexml;
Else {

Xmlstr = XMLHTTP. responsetext;
Alert (xmlstr );
VaR parser = new domparser ();

Xmldoc = parser. parsefromstring (xmlstr, "text/XML ");
}
Try {
Ready (xmldoc );
} Catch (e ){
Alert (E. Message );
}
}
}
}

VaR create = function (URL, R ){

Try {
// Firefox, opera 8.0 +, Safari
XMLHTTP = new XMLHttpRequest ();
} Catch (e ){

// Internet Explorer
Try {
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){

Try {
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){
Alert ("your browser does not support Ajax! ");
Return false;
}
}
}
XMLHTTP. onreadystatechange = R;
XMLHTTP. Open ("get", URL, true );
XMLHTTP. Send (null );

}
Create (URL, R );

}

// Application
Function Ready (xmldoc)
{


X = Xmldoc. getelementsbytagname ( " A " )[ 1 ];
Y = X. childnodes [ 0 ];
Txt = Y. nodevalue;
Alert (txt );

}
Getresult ( " ../XML. jsp " , Ready );

 

XML:

<R>
<A> FF </a>
<A> GGD </a>
</R>

The childnodes attribute returns the list of child nodes. <A> the element has only one subnode, that is, one text node.

The followingCodeRetrieve the text node of the <A> element:

 
X = xmldoc. getelementsbytagname ("A") [0]; y = x. childnodes [0];

The nodevalue attribute returns the text value of the text node:

 
X = xmldoc. getelementsbytagname ("title") [0]; y = x. childnodes [0]; TXT = Y. nodevalue;

Result: TXT = "GGD"

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.