注册 登录
编程论坛 JavaScript论坛

ajax处理XML格式时响应xhr.responseXML为根目录,但其的子对像firstChild为NULL

wangxianping 发布于 2011-09-18 17:28, 1248 次点击
<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>
  

 
3 回复
#2
wangxianping2011-09-18 23:02
xmlDoc.load("F:\\ASP\\jaxTree.xml");
#3
刘杰明2011-09-20 08:29
厉害....
#4
aspic2011-09-24 15:35
...应该没有权限读取吧。。。
1