js json转xml(可自定义属性,区分大小写)
直接上代码~
在网上找了很多例子,大多数不适用,而且麻烦,自己进行了一些调整,完成了一个可以区分大小写的json转xml的小功能~
功能实现js:
function CreatXmlDoc(obj){ this.tagName=obj.tagName; this.properties = obj.properties; var children=obj.children.map(function(item){ if(typeof item =="object") { item=new CreatXmlDoc(item) } return item }) this.children=children; } function loadXMLDoc(dname) { try {//Internet Explorer xmlDoc = 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.render=function(){ xmlDoc = loadXMLDoc(); var xe1 = xmlDoc.createElement(this.tagName); for(var p in this.properties){ xe1.setAttribute(p, this.properties[p]); } var children=this.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 doc=new CreatXmlDoc(eleJson);
SetupSerial=(new XMLSerializer()).serializeToString(doc.render());
var reg = new RegExp(' xmlns="http://www.w3.org/1999/xhtml"',"g");
// 替换转义字符
var reg1 = new RegExp('<',"g"); // <
var reg2 = new RegExp('>',"g"); // >
var reg3 = new RegExp('&',"g"); // &
var reg4 = new RegExp(''',"g"); // '
var reg5 = new RegExp('"',"g"); // "
SetupSerial=SetupSerial.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 type="text" name="hahha" title="哈哈"> <ProtocolList> <Protocol> <Name>onvif</Name> <UserName>admin</UserName> <PassWord>admin</PassWord> <Port>8000</Port> <MediaPort>8000</MediaPort> </Protocol> <Protocol> <Name>onvif</Name> <UserName>admin</UserName> <PassWord>admin</PassWord> <Port>8000</Port> <MediaPort>8000</MediaPort> </Protocol> <Protocol> <Name>onvif</Name> <UserName>admin</UserName> <PassWord>admin</PassWord> <Port>8000</Port> <MediaPort>8000</MediaPort> </Protocol> </ProtocolList> <Function> <PlayBack>onvif</PlayBack> <Other>rtsp</Other> </Function> </Setup>
大功告成!