已经玩了一段时间,但似乎无法弄明白。我想获取OverallScore标记,但当前代码生成一个TypeError:无法调用方法" getChildren"为null。需要帮忙。以下是我目前的代码:
function getDriverScores () {
var xml =
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
+"<soap:Body>"
+ "<GetDriverScoresInDateRangeResponse
xmlns=\"http://www.omnibridge.com/SDKWebServices/AssetData\">"
+ "<GetDriverScoresInDateRangeResult>"
+ "<DriverScore>"
+ "<DriverID>4</DriverID>"
+ "<OverallScore>99.05209</OverallScore>"
+ "<Distance>7283.8</Distance>"
+ "<Duration>683800</Duration>"
+ "<OverSpeedingScore>100</OverSpeedingScore>"
+ "<OverRevvingScore>100</OverRevvingScore>"
+ "<HarshBrakingScore>100</HarshBrakingScore>"
+ "
<OutOfGreenBandDrivingScore>99.64171</OutOfGreenBandDrivingScore>"
+ "<ExcessiveIdlingScore>94.67081</ExcessiveIdlingScore>"
+ "<HarshAccelerationScore>100</HarshAccelerationScore>"
+ "</DriverScore>"
+ "</GetDriverScoresInDateRangeResult>"
+ "</GetDriverScoresInDateRangeResponse>"
+"</soap:Body>"
+"</soap:Envelope>"
var options =
{
"method" : "post",
"contentType" : "text/xml",
"payload" : xml,
muteHttpExceptions:true
};
var xmlResult = XmlService.parse(xml).getRootElement();
var soapNamespace = xmlResult.getNamespace("soap");
var GetDriverScoresInDateRangeResponse = xmlResult.getChild("Body",
soapNamespace).getChildren()[0];
var GetDriverScoresInDateRangeResponseNamespace =
GetDriverScoresInDateRangeResponse.getNamespace();
var DriverScore =
GetDriverScoresInDateRangeResponse.getChild("DriverScore",
GetDriverScoresInDateRangeResponseNamespace).getText();
Logger.log(DriverScore);
}
答案 0 :(得分:0)
这次修改怎么样?我认为有几处修改,所以请将其视为其中之一。
DriverScore
检索GetDriverScoresInDateRangeResponseNamespace.getChildren()[0]
。OverallScore
DriverScore
var xmlResult = XmlService.parse(xml).getRootElement();
var soapNamespace = xmlResult.getNamespace("soap");
var GetDriverScoresInDateRangeResponse = xmlResult.getChild("Body", soapNamespace).getChildren()[0];
var GetDriverScoresInDateRangeResponseNamespace = GetDriverScoresInDateRangeResponse.getChildren()[0];
var GetDriverScoresInDateRangeResult = GetDriverScoresInDateRangeResponseNamespace.getChildren()[0];
var DriverScore = GetDriverScoresInDateRangeResult.getChildren();
DriverScore.forEach(function(e){
var name = e.getName()
if (name == "OverallScore") {
Logger.log("%s, %s", name, e.getText()) // OverallScore, 99.05209
}
});
xml
用于此修改过的脚本。如果我误解了你的问题,我很抱歉。