// 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"