Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value from XML string with Node.js

Tags:

node.js

xml

Let's say you have this xml:

<yyy:response xmlns:xxx='http://domain.com'> 
    <yyy:success> 
        <yyy:data>some-value</yyy:data> 
    </yyy:success> 
</yyy:response>

How would I retrieve the value in between <yyy:data> using Node.js?

Thanks.

like image 682
rexposadas Avatar asked Mar 08 '12 00:03

rexposadas


1 Answers

node-xml2js library will help you.

var xml2js = require('xml2js');

var parser = new xml2js.Parser();
var xml = '\
<yyy:response xmlns:xxx="http://domain.com">\
    <yyy:success>\
        <yyy:data>some-value</yyy:data>\
    </yyy:success>\
</yyy:response>';
parser.parseString(xml, function (err, result) {
    console.dir(result['yyy:success']['yyy:data']);
});
like image 147
Vadim Baryshev Avatar answered Oct 07 '22 17:10

Vadim Baryshev