如何使用 Node.js 从 XML 字符串中获取值 [英] How to get value from XML string with Node.js

查看:58
本文介绍了如何使用 Node.js 从 XML 字符串中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有这个 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>

如何使用 Node.js 检索 之间的值?

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

谢谢.

推荐答案

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']);
});

这篇关于如何使用 Node.js 从 XML 字符串中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆