如何解决无法使用Google Apps脚本解析SOAP XML响应 修改点:修改后的脚本:用于确认上述脚本的脚本:注意:参考:
我正在使用Google Apps脚本来调用SOAP XML Web服务,目的是将响应内容插入到Google工作表中。发出请求将返回带有有效数据值的预期响应,但是我很难解析响应。下面是我的功能...
function testFetch() {
var response = UrlFetchApp.fetch(setScadaHost(),setOptions());
var doc = XmlService.parse(response.getContentText());
var ns = XmlService.getNamespace(setNsScada());
var root = doc.getRootElement().getChild('scada-response',ns);
var entries = [];
for(var i in root) {
var id = root[i].getAttribute('node-id').getValue();
var td = root[i].getAttribute('trading-date').getValue();
var tp = root[i].getAttribute('trading-period').getValue();
var mw = root[i].getAttribute('generation').getValue();
entries.push(id,new Date(td),tp,mw);
}
shtSoap.getRange(shtSoap.getLastRow()+1,1,entries.length,4).setValues(entries);
}
shtSoap
在项目的其他位置定义,用于标识目标工作表。我收到的错误消息是“异常:范围内的行数必须至少为1”,并突出显示.setValues()
行。
如果我Logger.log(response);
得到的XML响应的结构如下:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<scada-response response-type="Scada Service" xmlns="[domainhost]/response/scada">
<node node-id="1st node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>1000</generation></time-stamp></trading-period></trading-date></node>
<node node-id="2nd node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>1200</generation></time-stamp></trading-period></trading-date></node>
<node node-id="3rd node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>1200</generation></time-stamp></trading-period></trading-date></node>
<node node-id="4th node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>800</generation></time-stamp></trading-period></trading-date></node>
</scada-response>
</soapenv:Body>
</soapenv:Envelope>
如果我Logger.log
在:
-
(entries)
在for
循环之后,我得到一个空数组。 -
(doc)
doc
声明后,出现以下错误“文档:没有DOCTYPE声明,根是[元素:]“
我还尝试在'scada-response'
声明中将'node'
换成root
,但得到相同的结果。
非常感谢您能提供任何帮助以了解我要去哪里的问题。
解决方法
我相信您的目标如下。
- 您要使用Google Apps脚本从问题中的XML数据中检索
node-id
,trading-date
,trading-period
和generation
的值。 - 您要将检索到的值放入电子表格。
修改点:
- 在您的XML数据中,
-
scada-response
是soapenv:Body
的子代。 -
node
是scada-response
的子代。 -
trading-date
是node
的子代。 -
trading-period
是trading-date
的子代。 -
generation
是time-stamp
的子代。
-
- 在您的脚本中,
var entries = [];
是循环中entries.push(id,new Date(td),tp,mw);
的一维数组。- 在这种情况下,我认为需要将
id,mw
到entries
的值作为一维数组。
- 在这种情况下,我认为需要将
当以上几点反映到您的脚本时,它如下所示。
修改后的脚本:
function testFetch() {
var response = UrlFetchApp.fetch(setScadaHost(),setOptions());
var doc = XmlService.parse(response.getContentText());
// --- I modified below script.
var ns1 = XmlService.getNamespace('soapenv','http://schemas.xmlsoap.org/soap/envelope/');
var ns2 = XmlService.getNamespace('[domainhost]/response/scada');
var nodeIds = doc.getRootElement().getChild('Body',ns1).getChild('scada-response',ns2).getChildren();
var entries = nodeIds.map(c => {
var tradingDate = c.getChild('trading-date',ns2);
var tradingPeriod = tradingDate.getChild('trading-period',ns2);
var id = c.getAttribute('node-id').getValue();
var td = tradingDate.getAttribute('value').getValue();
var tp = tradingPeriod.getAttribute('value').getValue();
var mw = tradingPeriod.getChild('time-stamp',ns2).getChild('generation',ns2).getValue();
return [id,mw];
});
// ---
shtSoap.getRange(shtSoap.getLastRow()+1,1,entries.length,4).setValues(entries);
}
用于确认上述脚本的脚本:
当您直接从XML数据测试上述脚本时,也可以使用以下示例脚本。在这种情况下,请将其复制并粘贴到脚本编辑器中并运行该功能。
function myFunction() {
var response = `<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<scada-response response-type="Scada Service" xmlns="[domainhost]/response/scada">
<node node-id="1st node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>1000</generation></time-stamp></trading-period></trading-date></node>
<node node-id="2nd node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>1200</generation></time-stamp></trading-period></trading-date></node>
<node node-id="3rd node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>1200</generation></time-stamp></trading-period></trading-date></node>
<node node-id="4th node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>800</generation></time-stamp></trading-period></trading-date></node>
</scada-response>
</soapenv:Body>
</soapenv:Envelope>`;
var doc = XmlService.parse(response);
var ns1 = XmlService.getNamespace('soapenv',mw];
});
console.log(entries)
}
注意:
- 请在V8运行时中使用以上修改的脚本。
参考:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。