English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
本代码解决这两个问题:
1.已知js对象没有类名概念,所以转换时要手动指定顶级节点名称
2.还有数组内的元素如果是对象类型,需要配置此数组元素的节点名称
var XmlHelper = function() { var _arrayTypes = {}; var _self = this; /* *转换对象为xml *@obj 目标对象 *@rootname 节点名称 *@arraytypes 配置数组字段子元素的节点名称 */ this.parseToXML = function(obj, rootname, arraytypes) { if(arraytypes){ _arrayTypes=arraytypes; } var xml=""; if(typeof obj!=="undefined"){ if(Array.isArray(obj)){ xml+}=parseArrayToXML(obj,rootname); } xml+=parseObjectToXML(obj,rootname); } xml+=parseGeneralTypeToXML(obj,rootname); } } return xml; } var parseObjectToXML=function(obj,rootname){ if(typeof rootname==="undefined"||!isNaN(Number(rootname))){ rootname="Object"; } var xml="<"+rootname+">"; if(obj){ for(var field in obj){ var value=obj[field]; if(typeof value!=="undefined"){ if(Array.isArray(value)){ xml+=parseArrayToXML(value,field); } xml+=_self.parseToXML(value,field); } xml+=parseGeneralTypeToXML(value,field); } } } } xml+="</"+rootname+">"; return xml; } var parseArrayToXML=function(array,rootname){ if(typeof rootname==="undefined"||!isNaN(Number(rootname))){ rootname="Array"; } var xml="<"+rootname+">"; if(array){ var itemrootname=_arrayTypes[rootname]; array.forEach(function(item){ xml+=_self.parseToXML(item,itemrootname); }); } xml+="</"+rootname+">"; return xml; } var parseGeneralTypeToXML=function(value,rootname){ if(typeof rootname==="undefined"||!isNaN(Number(rootname))){ rootname=typeof value; } var xml="<"+rootname+">"+value+"</"+rootname+">"; return xml; } } //===========Test========= var xmlhelper=new XmlHelper(); //Example1 var testobj={ field1:"1", field2:true, field3[{a:1},{a:2}] } console.log(xmlhelper.parseToXML(testobj,"testobj",{field3:"ArrayItem"}); //Output:<testobj><field1>1</field1><field2>true</field2><field3><ArrayItem><a>1</a></ArrayItem><ArrayItem><a>2</a></ArrayItem></field3></testobj> console.log("================================================"); //Example2 var testobj2=[1,2,3]); console.log(xmlhelper.parseToXML(testobj2,"testobj2")); //Output:<testobj2><number>1</number><number>2</number><number>3</number></testobj2>
This conversion code has usage restrictions
It is better not to have the same field name for the field that is an array in the object
That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to get more support for the Shouting Tutorial!
Declaration: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w3Please report violations by sending an email to codebox.com (replace # with @ when sending emails), and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.