日期:2014-05-16  浏览次数:20367 次

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