要求未定义xml2js

时间:2016-04-18 10:25:18

标签: xml node.js xslt xml-parsing require

我想使用库xml2js。我用npm安装在项目文件夹中。

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

执行节点myfile.js时出错:

d:\Profiles\user\ProjectIDE\Aproject\parseXML.js:3
parseString.(xml, function (err, result) {
            ^

SyntaxError: Unexpected token (
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

2 个答案:

答案 0 :(得分:0)

语法错误:

而不是:

parseString.(xml, function (err, result) {
    console.dir(result);
});

试试这个:

parseString(xml, function (err, result) {
    console.dir(result);
});

答案 1 :(得分:0)

这对我有用:

    var xml2js = require('xml2js');
    var parser = new xml2js.Parser();
    // var util = require('util');

    parser.parseString(xml, function(err, result){
      console.log(result);
      // In case you want to see the whole thing.
      // console.log(util.inspect(result, {showHidden:true, depth:null}); 
    });