JavascriptAJAX parses XML Code (compatible with FIREFOXIE)
The Code is as follows:
// 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:
The Code is as follows:
Ff
Ggd
The childNodes attribute returns the list of child nodes. Each element has only one subnode, that is, a text node.
The following code retrieves the text node of an 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"