六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 29|回复: 0

【整理】IE和FireFox都支持的AJAX解析XML的方法

[复制链接]

升级  24.67%

21

主题

21

主题

21

主题

秀才

Rank: 2

积分
87
 楼主| 发表于 2013-1-23 01:23:36 | 显示全部楼层 |阅读模式
========================AJAX=====================var xmlHttp = false;try {   xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}catch (e) {   try {   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");   }catch (e2) {    xmlHttp = false;   }}if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {   xmlHttp = new XMLHttpRequest();}function onIdcChanged(idcid){var requrl = "${ctx}/block/idc/block.do?method=getRoom&parentInventoryId="+idcid;xmlHttp.open("POST",requrl, true);xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");xmlHttp.onreadystatechange = update;xmlHttp.send(null);//为支持火狐加null}function update(){if(xmlHttp.readyState == 4) {   try{    var retXml = xmlHttp.responseText;       //alert(retXml);       //parase the xml,and then update the related box    if( retXml == "false" ){        //can not get the customer info        alert(retXml);       }else{        //get the html element        var room = document.getElementById("room");        clearSelect(room,true);        //get the xml data     var xmlDoc;        if (window.ActiveXObject)        {               xmlDoc=new ActiveXObject("Microsoft.XMLDOM");               xmlDoc.async=true;               xmlDoc.loadXML(retXml);        }        // code for Mozilla, Firefox, Opera, etc.        else if (document.implementation && document.implementation.createDocument)        {          var oParser=new DOMParser();                   xmlDoc=oParser.parseFromString(retXml,"text/xml");        }        else        {               alert('你的浏览器不支持这个脚本!');        }                  var items = xmlDoc.getElementsByTagName("rooms");     alert(items[1].getAttribute("name"));     for(var i=0;i<items.length;i++){      var id = items[i].getAttribute("id");      var name = items[i].getAttribute("name");      room.options.add(new Option(name,id));     }    }   }catch(e){    //alert(e);   }}}====================Method==================    public ActionForward getRoom(ActionMapping mapping, ActionForm form,            HttpServletRequest request,            HttpServletResponse response) {        String xml = "false";        Document document = DocumentHelper.createDocument();        Element root = document.addElement("root");        Element ele = null;        ele = root.addElement("rooms");        ele.addAttribute("id", "allroom");        MyUser user = (MyUser) request.getSession().getAttribute(MyConstant.SESS_USER);        Integer idcId = Integer.parseInt(request.getParameter("parentInventoryId"));        InventoryApi api = InventoryApi.getInstance();        List<InventoryItem> roomList = null;        roomList = api.getRoomListByIdc(idcId, user);        try {            for (int i = 0; roomList != null && i < roomList.size(); i++) {                ele = root.addElement("rooms");                ele.addAttribute("id", roomList.get(i).getId() + "");                ele.addAttribute("name", roomList.get(i).getName() + "");            }            xml = document.asXML();        } catch (Exception e) {            e.printStackTrace();        }        renderXML(response, xml);        return null;    }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表