6

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

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 Answer 1

8

I think that you have to check the value of 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]);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.