怎么做企业网站二维码,公众号开发工具下载,好看的标志设计,企业网站组网方案直接上代码~ 在网上找了很多例子#xff0c;大多数不适用#xff0c;而且麻烦#xff0c;自己进行了一些调整#xff0c;完成了一个可以区分大小写的json转xml的小功能~ 功能实现js#xff1a; function CreatXmlDoc(obj){this.tagNameobj.tagName;this.properties obj.p…直接上代码~ 在网上找了很多例子大多数不适用而且麻烦自己进行了一些调整完成了一个可以区分大小写的json转xml的小功能~ 功能实现js function CreatXmlDoc(obj){this.tagNameobj.tagName;this.properties obj.properties;var childrenobj.children.map(function(item){if(typeof item object){itemnew CreatXmlDoc(item)}return item})this.childrenchildren;}function loadXMLDoc(dname) {try {//Internet ExplorerxmlDoc new ActiveXObject(Microsoft.XMLDOM);} catch(e) {try {//Firefox, Mozilla, Opera, etc.xmlDoc document.implementation.createDocument(, , null);} catch(e) {alert(e.message)}}return xmlDoc;}CreatXmlDoc.prototype.renderfunction(){xmlDoc loadXMLDoc();var xe1 xmlDoc.createElement(this.tagName);for(var p in this.properties){xe1.setAttribute(p, this.properties[p]);}var childrenthis.children || [];children.forEach(function(child){var childEl(child instanceof CreatXmlDoc)? child.render():xmlDoc.createTextNode(child)xe1.appendChild(childEl);})return xe1;}// 封装调用函数 // 将json转成xml文档再转成字符串
var xmlToString function(eleJson) {var docnew CreatXmlDoc(eleJson);SetupSerial(new XMLSerializer()).serializeToString(doc.render());var reg new RegExp( xmlnshttp://www.w3.org/1999/xhtml,g);// 替换转义字符var reg1 new RegExp(lt;,g); // var reg2 new RegExp(gt;,g); // var reg3 new RegExp(amp;,g); // var reg4 new RegExp(apos;,g); // var reg5 new RegExp(quot;,g); // SetupSerialSetupSerial.replace(reg,).replace(reg1, ).replace(reg2, ).replace(reg3, ).replace(reg4, ).replace(reg5, );return SetupSerial; } 准备工作完成后就可以使用了 示例 var obj{tagName:Setup,properties:{type:text,name:hahha,title:哈哈},children:[{tagName:ProtocolList,children:[{tagName:Protocol,children:[{tagName:Name,children:[onvif]}, {tagName:UserName,children:[admin]}, {tagName:PassWord,children:[admin]}, {tagName:Port,children:[8000]}, {tagName:MediaPort,children:[8000]}, ]},{tagName:Protocol,children:[{tagName:Name,children:[onvif]}, {tagName:UserName,children:[admin]}, {tagName:PassWord,children:[admin]}, {tagName:Port,children:[8000]}, {tagName:MediaPort,children:[8000]}, ]},{tagName:Protocol,children:[{tagName:Name,children:[onvif]}, {tagName:UserName,children:[admin]}, {tagName:PassWord,children:[admin]}, {tagName:Port,children:[8000]}, {tagName:MediaPort,children:[8000]}, ]}, ]},{tagName:Function,children:[{tagName:PlayBack,children:[onvif] },{tagName:Other,children:[rtsp] }]}]}SetupSerial xmlToString(obj);
console.log(SetupSerial); 输出结果 Setup typetext namehahha title哈哈ProtocolListProtocolNameonvif/NameUserNameadmin/UserNamePassWordadmin/PassWordPort8000/PortMediaPort8000/MediaPort/ProtocolProtocolNameonvif/NameUserNameadmin/UserNamePassWordadmin/PassWordPort8000/PortMediaPort8000/MediaPort/ProtocolProtocolNameonvif/NameUserNameadmin/UserNamePassWordadmin/PassWordPort8000/PortMediaPort8000/MediaPort/Protocol/ProtocolListFunctionPlayBackonvif/PlayBackOtherrtsp/Other/Function
/Setup 大功告成 转载于:https://www.cnblogs.com/zh-1721342390/p/9894731.html