【整理】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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值