js生成xml文件,兼容ie和firefox
程序员文章站
2022-06-04 07:57:00
...
<script> function createXML(){ //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("", "doc", null); var oXmlDom = document.implementation.createDocument("","doc",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='ddddd'; node = document.createElement("ctype"); node.textContent='aaaa';r2.appendChild(node); node = document.createElement("maxUsers"); node.textContent='eeee';r2.appendChild(node); node = document.createElement("welcomes"); node.textContent='gggg';r2.appendChild(node); node = document.createElement("description"); node.textContent='wwwww';r2.appendChild(node); node = document.createElement("status"); node.textContent='ssss';r2.appendChild(node); node = document.createElement("startTime"); node.textContent='yyyy';r2.appendChild(node); node = document.createElement("closeTime"); node.textContent='iiiii';r2.appendChild(node); node = document.createElement("createTime"); node.textContent="llll";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('wwww'); var nametext=xmlDoc.createTextNode('ddddd'); var welcomestext=xmlDoc.createTextNode('ssss'); var descriptiontext=xmlDoc.createTextNode('qqqq'); var startTimetext=xmlDoc.createTextNode('tttt'); var closeTimetext=xmlDoc.createTextNode('rrrrrr'); 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); alert(xmlDoc.xml); } return xmlDoc; } createXML(); </script>
推荐阅读
-
JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)
-
JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)
-
禁用Tab键JS代码兼容Firefox和IE_javascript技巧
-
js生成xml文件,兼容ie和firefox
-
window.event.keyCode兼容IE和Firefox实现js代码_javascript技巧
-
禁用Tab键JS代码兼容Firefox和IE_javascript技巧
-
js实现连续英文字符自动换行兼容ie6 ie7和firefox_javascript技巧
-
Firefox和IE浏览器兼容JS脚本写法小结_javascript技巧
-
js实现连续英文字符自动换行兼容ie6 ie7和firefox_javascript技巧
-
js 兼容多浏览器的回车和鼠标焦点事件代码(IE6/7/8,firefox,chrome)_javascript技巧