How to get value from XML string with Node.js
You can do it easier with camaro
const camaro = require('camaro')
const xml = `<yyy:response xmlns:xxx='http://example.com'> <yyy:success><yyy:data>some-value</yyy:data> </yyy:success></yyy:response>`
const template = {
data: '//yyy:data'
}
console.log(camaro(xml, template))
Output:
{ data: 'some-value' }
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']);
});
You don't have to parse it for data extraction, simply use regexp :
str = "<yyy:response xmlns:xxx='http://example.com'> <yyy:success><yyy:data>some-value</yyy:data> </yyy:success></yyy:response>";
var re = new RegExp("<yyy:data>(.*?)</yyy:data?>", "gmi");
while(res = re.exec(str)){ console.log(res[1])}
// some-value