用nodejs xml2js读取xml文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/i042416/article/details/88575142

我有这样一个存储了配置信息的xml文件:

clipboard1

读取这个配置文件的JavaScript代码,使用了nodejs里的xml2js模块:

const fs = require('fs');
var path = require('path');

var configPath = path.join(__dirname, 'koiFieldList.xml');
var configString = fs.readFileSync(configPath, 'utf8');

var parseString = require('xml2js').parseString;

var siteDedicatedList = require("./sourceDedicatedFieldList.json");

function getAllFieldList(callback){
    parseString(configString, function (err, result) {
        if(err)
            throw err;
        callback(result);
    });
}


module.exports = {
    getAllFieldList: getAllFieldList
 };

如何消费这个getAllFieldList?

var koiFieldListAccessTool = require("../../control/koiFieldListAccessTool.js");

koiFieldListAccessTool.getAllFieldList(function(result){
          console.log(result);
        }
      );

测试结果,能够正常工作:

clipboard2

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

猜你喜欢

转载自blog.csdn.net/i042416/article/details/88575142