结合上一篇思路
代码编写如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">
//ajax无刷新方式获得第n页信息
function showxml(){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState==4){
//xhr.responseXML;//类比:Document对象
var xmldom=xhr.responseXML;
var messages=xhr.responseXML.firstChild;
var s="";
//console.log(message);
//console.log(message.childNodes);
var msgs=xmldom.getElementsByTagName('msg');
for(var i=0;i<msgs.length;i++){
var sder=msgs[i].getElementsByTagName('sender')[0].firstChild.nodeValue;
var jie=msgs[i].getElementsByTagName('jie')[0].firstChild.nodeValue;
var cnt=msgs[i].getElementsByTagName('content')[0].firstChild.nodeValue;
s+="发送者:"+sder+",接收者:"+jie+",消息:"+cnt+"<br/>";
}
document.getElementById('result').innerHTML=s;
// msgs[0]-->元素节点对象
//for(var k in msgs[0]){
//console.log(k);//输出元素节点对象的成员名称
//}
}
}
xhr.open('get','./js_09.xml');
xhr.send(null);
}
</script>
<style type="text/css">
h2{width:700px; margin:auto;
background-color:pink;text-align:center;}
</style>
</head>
<body>
<h2>实现对xml的接收处理</h2>
<input type="button" value="请求" onclick="showxml()"/>
<div id="result"></div>
</body>
</html>