读取“NodeJs”中的写入XML节点值

时间:2021-08-06 17:02:13

Can anyone guide me with how to read/ write XML nodevalues parsed by xml2js.Parser() in 'NodeJS'? So far my code is as flows:

任何人都可以指导我如何读取/写入由'NodeJS'中的xml2js.Parser()解析的XML节点值?到目前为止,我的代码是流程:

var parser = new xml2js.Parser();
fs.readFile( './foo.xml', function(err, data) {
    parser.parseString(data, function (err, result) {
        console.dir(result);
    });
});

I want to read the values of result as follows

我想读取结果的值如下

result.to

my XML:

<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

1 个解决方案

#1


8  

I think that you have to check the value of result.note.to[0] :

我认为你必须检查result.note.to [0]的值:

xml2js = require('xml2js');
fs = require('fs');

var parser = new xml2js.Parser();
fs.readFile( './foo.xml', function(err, data) {
    parser.parseString(data, function (err, result) {
        console.dir(result.note.to[0]);
    });
});

#1


8  

I think that you have to check the value of result.note.to[0] :

我认为你必须检查result.note.to [0]的值:

xml2js = require('xml2js');
fs = require('fs');

var parser = new xml2js.Parser();
fs.readFile( './foo.xml', function(err, data) {
    parser.parseString(data, function (err, result) {
        console.dir(result.note.to[0]);
    });
});