如何在node.js中将动态创建的JSON对象转换为XML?
我必须根据包含嵌套对象列表的可用对象动态创建JSON对象。
var Obj = {
root: {
Door_Keeper: {
ID: {
'#list': [{
Key: {
Name: "Door_Keeper_ID",
Value: DoorKeeper.dkId
}
}, {
Key: {
Key_Name: "ID",
Key_Value: DoorKeeper.id.Id
}
}]
},
Name: doorKeeper.dkName,
Description: doorKeeper.dkId,
Settings: dkSettings,
'#list':
//Here I have list of objects which will be added dynamically
}
}
};
我想从node.js中的上述JSON对象生成XML字符串
最佳答案
使用npm库js2xml,它将像
var Js2Xml = require("js2xml").Js2Xml;
var obj = ...... //as given by you in the question
var js2xml = new Js2Xml("root", obj);
js2xml.toString();