zhyiwww
用平实的笔,记录编程路上的点点滴滴………
posts - 536,comments - 394,trackbacks - 0

我今天遇到了一个问题,在用post发送一个请求到服务器,然后,返回来了XML的串数据,我想通过response.responseXML来取得XML的Document对象,来实现用Javascript对XML文档的解析,但是,我就只能用response.responseText取得文本数据,通过response.responseXML取得的对象为null.结果一直也没有搞明白。
javascript的代码如下(用的prototype1.6.js):

function findpoi(){
    //alert("find ");  
    var pname=$F('pnid');
    alert(pname);
    
    var url="poiSearch.do";
    
    var opt = {
     method:'post',
     
     //contentType:'application/xml',
     //requestHeaders:'text/xml',
     onComplete:function(transport){
      alert(transport.status);
      document.title="ok";
      if(200==transport.status){
       
       //document.title="ok";
       var cntTxt=transport.responseText;
       alert(cntTxt);
       
       var dobj = transport.responseXML;
       alert("xml is :  "+dobj);
      (此处总是取得的是null对象)
       
       //var pl = cntTxt.getElementByName("poi");
       //alert(pl);
       
       var ajObj = Ajax.getTransport();
       alert(ajObj);
        
            
      }
     },
     postBody:"?city=beijing&keyword=kfc"
     
    };
    
    var ajax=new Ajax.Request(url,opt);
    
   }

我的struts action的代码如下:

public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  DynaActionForm poiForm = (DynaActionForm) form;

  String city = (String) poiForm.get("city");
  String keyword = (String) poiForm.get("keyword");


  
  Search bs = new Search();
  bs.baseSearch(city + "/" + keyword);

  PoiDAO poidao = new PoiDAO();
  int count = 10;
  int page = 1;
  ArrayList arry = poidao.by_key_search("北京", "长安商场", count,
    page);
  System.out.println(arry.size());
  
  Document doc= DocumentFactory.getInstance().createDocument();
  doc.setXMLEncoding("UTF-8");
  Element root = doc.addElement("pois");
  
  root.addElement("poi")
  .addAttribute("pid", "1155")
  .addAttribute("longitude", "116.5864")
  .addAttribute("latitude", "39.8195");  
  
  
  ListIterator iter = arry.listIterator();
  while(iter.hasNext()){
  
   Poi p = (Poi)iter.next();
   //float lon = p.getLatitude();
   int id=p.getPoi_id();
   double lon=p.getLongitude();
   double lat = p.getLatitude();
   
   System.out.println("id is :  "+id);
   System.out.println("lon : " + lon);
   
   Element poi=root.addElement("poi")
    .addAttribute("pid", String.valueOf(p.getPoi_id()))
    .addAttribute("longitude", String.valueOf(lon))
    .addAttribute("latitude", String.valueOf(lat));   
  }
  
  try {
   PrintWriter w = response.getWriter();
   
   XMLWriter writer = new XMLWriter(w);
   writer.write(doc);
   
   writer.close();
   w.close();
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  return null;
 }

下午的时候,总是在试图通过设置contentType来实现,结果是失败了。回来的路上,我就一直在想,能取到XML的串,不能取到XML的对象。那么也就是说,返回的数据是串,也就是文本类型。突然,我的脑海里闪现了一个问题,是不是返回的contentType没有设置成xml.
所以,我就在action里面,添加了
response.setContentType("text/xml");
重新测试,果然成功。
response.responseXML取到了对象。

问题就这样得到了解决。

问题总结:
如果想要通过AJAX的response.responseXML取得XML Document对象,那么就要在服务器的相应的时候设置ContextType为text/xml,否则的话,就只能去得到xml的文本串,不能得到DOM对象。




|----------------------------------------------------------------------------------------|
                           版权声明  版权所有 @zhyiwww
            引用请注明来源 http://www.blogjava.net/zhyiwww   
|----------------------------------------------------------------------------------------|
posted on 2008-05-21 21:11 zhyiwww 阅读(2498) 评论(3)  编辑  收藏 所属分类: j2ee

FeedBack:
# re: AJAX中返回XML串但是不能返回XML DOM对象问题的解决
2008-08-04 17:58 | 小陈
扯淡嘛~~~~~~  回复  更多评论
  
# re: AJAX中返回XML串但是不能返回XML DOM对象问题的解决
2008-08-21 16:29 | Just
writer.write(doc);
write方法中真能用Object做参数吗?我用的JDK6,没有这个参数的方法啊,楼主怎么实现的?有兴趣的话加我QQ5142035聊下  回复  更多评论
  
# re: AJAX中返回XML串但是不能返回XML DOM对象问题的解决
2008-08-27 22:58 | zhyiwww
@Just
我用的是dom4J  回复  更多评论
  

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


网站导航: