javascript 在ie、firefox 创建xml


/****************AJAX ********************/
function createXML(room){

//var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var xmlDoc;
var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') ;
var ie = (typeof window.ActiveXObject != 'undefined') ;

if (moz)
{
xmlDoc = document.implementation.createDocument("", "", null);
var oXmlDom = document.implementation.createDocument("","",null);
var oRoot = oXmlDom.documentElement;
var testnode = document.createElement("req");
oRoot = testnode;
var r2 = oXmlDom.documentElement;
var r2_text = document.createElement("chatRoom");
r2=r2_text;

var xs = new XMLSerializer();

var node = document.createElement("cname");
node.textContent=room.cname;

node = document.createElement("ctype");
node.textContent=room.ctype;r2.appendChild(node);
node = document.createElement("maxUsers");
node.textContent=room.maxUsers;r2.appendChild(node);
node = document.createElement("welcomes");
node.textContent=room.welcomes;r2.appendChild(node);
node = document.createElement("description");
node.textContent=room.description;r2.appendChild(node);
node = document.createElement("status");
node.textContent=room.status;r2.appendChild(node);
node = document.createElement("startTime");
node.textContent=room.startTime;r2.appendChild(node);
node = document.createElement("closeTime");
node.textContent=room.closeTime;r2.appendChild(node);
node = document.createElement("createTime");
node.textContent="";r2.appendChild(node);

oRoot.appendChild(r2);

alert(xs.serializeToString( oRoot ));
}
else if (ie)
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
//return xmldoc;

//xmlDoc.async=false;
//创建两条处理指令
var newPI=xmlDoc.createProcessingInstruction("xml","version='1.0' encoding='utf-8'");
xmlDoc.appendChild(newPI);
//创建根元素
var documentElement=xmlDoc.createElement("req");
xmlDoc.appendChild(documentElement);
//创建CDATA
//var newCD=xmlDoc.createCDATASection("This is a CDATASection node");
//xmlDoc.documentElement.appendChild(newCD);

//创建元素invoice,其子元素customer,再为customer加上属性,
var chatRoom=xmlDoc.createElement("chatRoom");
var id=xmlDoc.createElement("id");
var cname=xmlDoc.createElement("cname");
var ctype=xmlDoc.createElement("ctype");
var maxUsers=xmlDoc.createElement("maxUsers");
var welcomes=xmlDoc.createElement("welcomes");
var description=xmlDoc.createElement("description");
var status=xmlDoc.createElement("status");
var startTime=xmlDoc.createElement("startTime");
var closeTime=xmlDoc.createElement("closeTime");
var createTime=xmlDoc.createElement("createTime");

chatRoom.appendChild(id);
chatRoom.appendChild(cname);
chatRoom.appendChild(ctype);
chatRoom.appendChild(maxUsers);
chatRoom.appendChild(welcomes);
chatRoom.appendChild(description);
chatRoom.appendChild(status);
chatRoom.appendChild(startTime);
chatRoom.appendChild(closeTime);
chatRoom.appendChild(createTime);

//创建text节点,作为id元素的内容
var idtext=xmlDoc.createTextNode(room.id);
var nametext=xmlDoc.createTextNode(room.cname);
var welcomestext=xmlDoc.createTextNode(room.welcomes);
var descriptiontext=xmlDoc.createTextNode(room.description);
var startTimetext=xmlDoc.createTextNode(room.startTime);
var closeTimetext=xmlDoc.createTextNode(room.closeTime);

id.appendChild(idtext);
cname.appendChild(nametext);
ctype.appendChild(xmlDoc.createTextNode("1"));
maxUsers.appendChild(xmlDoc.createTextNode("500"));
welcomes.appendChild(welcomestext);
description.appendChild(descriptiontext);

status.appendChild(xmlDoc.createTextNode("1"));
createTime.appendChild(xmlDoc.createTextNode(new Date()));
startTime.appendChild(startTimetext);
closeTime.appendChild(closeTimetext);

xmlDoc.documentElement.appendChild(chatRoom);
}
return xmlDoc;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值