如何将动态创建的JSON对象转换为node.js中的XML?

时间:2015-06-22 23:30:26

标签: javascript json node.js

如何在node.js中将动态创建的JSON对象转换为XML?

我必须根据包含嵌套对象列表的available来动态创建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字符串

1 个答案:

答案 0 :(得分:1)

使用npm库,js2xml,它就像是,

var Js2Xml = require("js2xml").Js2Xml;
var obj = ...... //as given by you in the question
var js2xml = new Js2Xml("root", obj);
js2xml.toString();