node.js的xml和json相互转换-xml2js

首先安装xml2js

npm install xml2js

引入包:

const xml2js = require('xml2js');

xml转json

var xml = "Hello xml2js!"
var parseString = require('xml2js').parseString;
parseString(xml, function (err, result) {
    console.dir(result);
});

json转xml (Since 0.4.0, objects can be also be used to build XML:)

var obj = {name: "Super", Surname: "Man", age: 23};
 
var builder = new xml2js.Builder();
var xml = builder.buildObject(obj);

开始需求只是xml转json,之后修改完后又要json转xml。

参考文档: https://www.npmjs.com/package/xml2js

你可能感兴趣的:(前端--漂亮大气)