Home > Web Front-end > JS Tutorial > Javascript (AJAX) parsing XML code (compatible with FIREFOX/IE)_javascript skills

Javascript (AJAX) parsing XML code (compatible with FIREFOX/IE)_javascript skills

WBOY
Release: 2016-05-16 18:23:15
Original
849 people have browsed it
复制代码 代码如下:

//导入js文件
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("您的浏览器不支持AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange = r;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
create(url, r);
}

//应用
function ready(xmlDoc)
{

x=xmlDoc.getElementsByTagName("a")[1];
y=x.childNodes[0];
txt=y.nodeValue;
alert(txt);
}
getResult("../xml.jsp",ready);

xml:
复制代码 代码如下:

childNodes 属性返回子节点的列表。 元素只有一个子节点,即一个文本节点。
下面的代码检索
元素的文本节点:
x=xmlDoc.getElementsByTagName("a")[0];
y=x.childNodes[0];
nodeValue 属性返回文本节点的文本值:
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
txt=y.nodeValue;
结果:txt = "ggd"
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template