Javascript 對象轉(zhuǎn)換 XML 字符串的方法
var _arrayTypes = {}
var _self = this;
/*
*轉(zhuǎn)換對象為xml
*@obj 目標對象
*@rootname 節(jié)點名稱
*@arraytypes 配置數(shù)組字段子元素的節(jié)點名稱
*/
this.parseToXML = function (obj, rootname, arraytypes) {
if (arraytypes) {
_arrayTypes = arraytypes;
}
var xml = "";
if (typeof obj !== "undefined") {
if (Array.isArray(obj)) {
xml += parseArrayToXML(obj, rootname);
} else if (typeof obj === "object") {
xml += parseObjectToXML(obj, rootname);
} else {
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);
} else if (typeof value === "object") {
xml += _self.parseToXML(value, field);
} else {
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;
}
}
示例:
var xmlhelper = new XmlHelper();
//示例1
var testobj = {
field1: "1",
field2: true,
field3: {
a: 123,
b: 123
},
children: [{
a: 1
}, {
a: 2
}]
}
console.log(xmlhelper.parseToXML(testobj, "coreplus-root", {
children: "menu"
}));
//輸出:<testobj><field1>1</field1><field2>true</field2><field3><ArrayItem><a>1</a></ArrayItem><ArrayItem><a>2</a></ArrayItem></field3></testobj>
console.log("================================================");
//示例2
var testobj2 = [1, 2, 3];
console.log(xmlhelper.parseToXML(testobj2, "testobj2"));
//輸出:<testobj2><number>1</number><number>2</number><number>3</number></testobj2>