node.js - 在 Bot Builder 中从 URL 解析 XML 数据
问题描述
请问如何在 Bot Builder 中解析来自 URL 的 XML 数据?
我试过 xml2js 但没有用,还有 xmlhttprequest。
这里有我的源代码
var xmldata = "https://mvponduty.mvpondutyonmicrosoft.com/xml/data.xml";
console.log(xmldata);
parseString(xmldata, function (err, result) {
// Result contains XML data in JSON format
context.sendActivity(result.toString());
});
谢谢你。
解决方案
我为您做了一些测试,您可以尝试以下代码从 URL 获取 xml 并将其解析为 bot 中的 JSON:
this.onMessage(async (context, next) => {
var xmlURL = 'https://andyapi2.azurewebsites.net/test/test.xml';
let xml2js = require('xml2js');
const axios = require('axios');
var resp = '';
await axios.get(xmlURL)
.then(response => {
xml2js.parseString(response.data, function(_err, result) {
console.log(result);
resp = result;
});
})
.catch(error => {
console.log(error);
});
await context.sendActivity(resp.note.body[0]);
await next();
});
XML 数据:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
结果 :
推荐阅读
- node.js - 在 nodejs 客户端/服务器中通过 TCP 发送查询结果的问题
- xml - 现有代码中的大写集成 (XSLT 1.0)
- javascript - ReactJs - 在 li 元素上的单击事件上,不会立即添加该类
- swift - SwiftUI Core Data Master/Detail
- ruby - Ruby 命名空间与在方法中定义并使用 attr 的模块类实例变量冲突:
- java - 如何让一个被发现为真的方法在下面显示另一个方法?爪哇
- c# - 如何对以下意外事件进行编程?
- c++ - C++ 中的异或加密
- c# - 使用 Blazor 服务器端组件的 MVC 应用程序中的防伪令牌验证
- rust - 为什么这里会出现 Rust 可变借用?