i'm using google apps script to call a SOAP XML web service with the intent on inserting the response content into a google sheet. making the request returns the expected response with valid data values however i'm having difficulty parsing the response. below is my function...
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 is defined elsewhere in the project, identifying the destination worksheet. the error message i get back is "Exception: The number of rows in the range must be at least 1" and highlights the .setValues() row.
if i Logger.log(response); i get an XML response structured like so:
<?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>
if i Logger.log on:
(entries)after theforloop, i get an empty array.(doc)after thedocdeclaration, i get the following error "Document: No DOCTYPE declaration, Root is [Element: <soapenv:Envelope [Namespace: http://schemas.xmlsoap.org/soap/envelope/]/>]"
i have also tried swapping out 'scada-response' for 'node' in the root declaration but get the same results.
any help you can offer to understand where i'm going wrong is greatly appreciated.