ajax处理XML格式时响应xhr.responseXML为根目录,但其的子对像firstChild为NULL
<html><head>
<meta http-equiv="content-type" keywords="ajax" description="ajax 描述" content="text/html;charset=UTF-8">
<title>ajax的树结构</title>
</head>
<script language="JavaScript">
var xhr=false;
function request(){
createXMLHttpRequest();
xhr.open("post","F:\\ASP\\jaxTree.xml",true);
xhr.onreadystatechange=callBack;
xhr.send(null);
}
function createXMLHttpRequest(){
if(window.ActiveXObject){
xhr=new ActiveXObject('Microsoft.XMLHTTP');
}else if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
}
var callBack=function (){
if(xhr.readyState==4){
if(xhr.status==0){
createTree(xhr.responseXML);
xmlDoc=xhr.responseXML;
alert(xmlDoc.documentElement);
alert(xmlDoc.firstChild);
}
}
}
var createTree=function (xmlDoc){
alert(xmlDoc.hasChildNodes);
}
</script>
<body onload="request();">
<div id='tree' style='float:left;width:300'>xml格式</div>
<div id="message" style="float:right;width:500">文本</div>
</body>
</html>
//jaxTree.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
<type>
<computer>硬件组装 </computer>
<soft>C#编程</soft>
</type>
<type>
<computer>硬件维护</computer>
<soft>JavaScript编程</soft>
</type>
<type>
<computer>硬件知识</computer>
<soft>java编程</soft>
</type>
</books>