假设你有这个xml:
<yyy:response xmlns:xxx='http://domain.com'>
<yyy:success>
<yyy:data>some-value</yyy:data>
</yyy:success>
</yyy:response>
Run Code Online (Sandbox Code Playgroud)
我如何<yyy:data>使用Node.js 检索两者之间的值?
谢谢.
Vad*_*hev 12
node-xml2js库可以帮到你.
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']);
});
Run Code Online (Sandbox Code Playgroud)
您可以使用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))
Run Code Online (Sandbox Code Playgroud)
输出:
{ data: 'some-value' }
Run Code Online (Sandbox Code Playgroud)