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


========================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;
    }

你可能感兴趣的:(Ajax,xml,IE,Microsoft,firefox)