JavaScript中的Alexa XML子字符串无法正常工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript中的Alexa XML子字符串无法正常工作相关的知识,希望对你有一定的参考价值。

我正在尝试打印xml字符串,该字符串报告具有相同标签的不同对象的一些数据。但是,当我尝试使用循环使用Substring打印字符串时,它将多次打印相同的信息。

    var num = 5; var respString = "**html string**" while(num > 0){

    var a="";
    if(num == 5)    
        a=respStr1.substring(respStr1.lastIndexOf("<?xml version="1.0" encoding="UTF-8"?>") +42, respStr1.lastIndexOf("</prtg-version>"));

    //var string= respStr1.substring(length+42+15);
    var string= respStr1.substring(respStr1.lastIndexOf("<item>") +6, respStr1.lastIndexOf("</item>"));

    var length=string.length+ a.length+107;
            var sensName="";
            var server="";
            var status="";
            var probe="";
            var message="";

            sensName= string.substring(string.lastIndexOf("<name>") +6, string.lastIndexOf("</name>"));

            status= string.substring(string.lastIndexOf("<status>") +8, string.lastIndexOf("</status>"));

            server= string.substring(string.lastIndexOf("<device>") +8, string.lastIndexOf("</device>"));

            probe= string.substring(string.lastIndexOf("<probe>") +7, string.lastIndexOf("</probe>"));

            message=string.substring(string.lastIndexOf("<message_raw>") +13, string.lastIndexOf("</message_raw>"));

            finalString += "Il sensore "+sensName+ ", sul server "+ server +" del cliente "+ probe + " è nello stato "+ status + ". Il messaggio di errore è: " + message + ". "; 
           respStr1= respStr1.substring(length);
           num--;}

如何避免出现问题?

答案

使用XML解析是正确的方式-javascript(至少浏览器,我认为Alexa也具有)是内置的:

const respString = '<?xml version="1.0" encoding="UTF-8"?>' +
'<prtg-version>' +
'    <items>' +
'        <item>' +
'            <name>Name1</name>' +
'            <status>Status1</status>' +
'            <device>Device1</device>' +
'            <probe>Probe1</probe>' +
'            <message_raw>Message1</message_raw>' +
'        </item>' +
'        <item>' +
'            <name>Name2</name>' +
'            <status>Status2</status>' +
'            <device>Device2</device>' +
'            <probe>Probe2</probe>' +
'            <message_raw>Message2</message_raw>' +
'        </item>' +
'    </items>' +
'</prtg-version>';

const parser = new DOMParser();
const xmlDoc = parser.parseFromString(respString, "text/xml");
const items = xmlDoc.getElementsByTagName("item");
for (var i = 0; i < Math.min(5, items.length); i++) {
  const item = items[i];
  const name = item.getElementsByTagName('name')[0].textContent;
  const status = item.getElementsByTagName('name')[0].textContent;
  const device = item.getElementsByTagName('device')[0].textContent;
  const probe = item.getElementsByTagName('probe')[0].textContent;
  const message_raw = item.getElementsByTagName('message_raw')[0].textContent;
  const final_string = `Il sensore ${name}, sul server ${device} del client ${probe} e nello stato ${status}. Il messaggio di errore e: ${message_raw}`
  console.log(final_string);
}


> Il sensore Name1, sul server Device1 del client Probe1 e nello stato Name1. Il messaggio di errore e: Message1
> Il sensore Name2, sul server Device2 del client Probe2 e nello stato Name2. Il messaggio di errore e: Message2

以上是关于JavaScript中的Alexa XML子字符串无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

javascript Alexa测验

javascript Alexa - 对AudioPlayer.PlaybackStarted的响应导致错误

Alexa - 如何向用户发送图像?

有没有办法从 Alexa localisation.js 文件中存储在 s3 中的文本文件中获取字符串(数据)?

用python更改xml文件中的子字符串

如何检查字符串是不是包含 JavaScript 中的子字符串?