super

AJAX返回XML格式文本的读取方法

对于一个AJAX请求
如果返回的是标准的XML(有<?xml version="1.0" encoding="UTF-8"?>,并且ContentType = "text/xml"),则直接操作xmlhttp.responseXML应该是可以的,比如:
var requestMsg=xmlhttp.responseXML;
alert(requestMsg.getElementsByTagName("book").length);

如果不是标准的XML.则返回的信息实际上是以文本的方式表示的,必须从xmlhttp.responseText中取得数据,方式如下:
var requestMsg=getXMLDoc(originalRequest.responsetext);
alert(requestMsg.getElementsByTagName("book").length);


getXMLDoc方法如下:

 function getXMLDoc(xmlText){
  if(window.ActiveXObject){
   xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
   xmlDoc.async=false;
   xmlDoc.onreadystatechange = function()
   {
    //if(xmlDoc.readyState == 4) doAction();
   }
   xmlDoc.loadXML(xmlText);
  }else if(document.implementation&&document.implementation.createDocument){
   xmlDoc=document.implementation.createDocument('','',null);
   //xmlDoc.onload=doAction();
   xmlDoc.loadXML(xmlText);
  }else return null;
  return xmlDoc;
 }

posted on 2006-11-27 16:02 王卫华 阅读(3732) 评论(4)  编辑  收藏 所属分类: AJAX

Feedback

# re: AJAX返回XML格式文本的读取方法 2007-03-08 18:47 11

111111111111111  回复  更多评论   

# re: AJAX返回XML格式文本的读取方法 2007-07-12 23:04 文字

不错,看过用过  回复  更多评论   

# re: AJAX返回XML格式文本的读取方法 2010-04-08 13:26 得到

字体太小了  回复  更多评论   

# re: AJAX返回XML格式文本的读取方法 2010-07-14 20:33 Help

//xmlDoc.onload=doAction();

这句为什么要注释掉??  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航: