首页 > 解决方案 > 在 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());
   });

谢谢你。

标签: node.jsbotframeworkazure-bot-service

解决方案


我为您做了一些测试,您可以尝试以下代码从 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>

结果 :

在此处输入图像描述


推荐阅读