Copy Code code as follows:
Import 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:
Copy Code code as follows:
<r>
<a>ff</a>
<a>ggd</a>
</r>
The ChildNodes property returns a list of child nodes. The <a> element has only one child node, that is, a text node.
The following code retrieves the text node of the <a> element:
X=xmldoc.getelementsbytagname ("a") [0];
Y=x.childnodes[0];
The NodeValue property returns the text value of the text node:
X=xmldoc.getelementsbytagname ("title") [0];
Y=x.childnodes[0];
Txt=y.nodevalue;
Result: txt = "GGD"