[使用JavaScript的Alexa中的XML解析器问题

问题描述 投票:0回答:1
    var xml = require('XML-Parser-master');
        const fs = require('fs');
 var responseString = 'html string';

        var finalString="";


        var xmlDoc = new xml.DOM(xml.parse(responseString)); 

        const items =xmlDoc.document.getElementsByTagName("item");
        for (var i = 0; i < items.length ; i++) {
          const item = items[i];
          var name = (item.getElementsByTagName("name")[0]),
             status = (item.getElementsByTagName('status')[0]),
             device = (item.getElementsByTagName('device')[0]),
             probe = (item.getElementsByTagName('probe')[0]),
             message_raw = (item.getElementsByTagName('message_raw')[0]);

            finalString += "Il sensore "+name+ ", sul server "+device+ " del  "+probe+ " è nello stato"+status+". Il messaggio di errore è: "+ message_raw+". ";

        }

          });
           res.on('end', function(res)
            {
                const speechOutput = finalString;
                console.log("==> Answering: ",speechOutput); 
                mythis.emit(':tell', speechOutput);
            });

问题是,当它应该在名称,状态,设备,探针和message_raw中添加不同的字符串时,它将返回“ [object Object]”。示例:

il sensore [对象对象],sul服务器[对象对象] del [对象对象] – nello stato [对象对象]。错误:没有定义。”}

使用的解析器是:https://github.com/MauriceConrad/XML-Parser#readme

javascript xml xml-parsing alexa alexa-skill
1个回答
0
投票

查看XML Parse模块XML Parse npm module docs之后,看起来您只需要将变量设置为对象的text属性即可。

例如

var name = (item.getElementsByTagName("name")[0]).text;
© www.soinside.com 2019 - 2024. All rights reserved.