XML解析器问题。如何将节点另存为字符串

问题描述 投票:0回答:1

这是我的代码,我有问题:

   var responseString= '<?xml version="1.0" encoding="UTF-8"?>' +
'    <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>' ;


    var xml = require('XML-Parser-master');
            const fs = require('fs');

            //const parser = new DOMParser();
            var xmlDoc = new xml.DOM(xml.parse(responseString)); 

            const items =xmlDoc.document.getElementsByTagName('item');
            for (var i = 0; i < items.length ; i++) {
              var 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]);

为什么当我尝试打印时,例如名称,结果是“ [Object object]?感谢您的帮助

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

我通过如下所示的修改来测试了相同的代码,但效果很好。试试这个。

© www.soinside.com 2019 - 2024. All rights reserved.