请有人帮助我,我被困住了,我试图解析XML数据并将其插入Google表格中,我可以正确获取XML,但是解析后,我得到的null为“ getChild”。
这是XML的一部分:
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:etaWebServicesResponse xmlns:ns1="http://ws.ABC.com/ABCws.wsdl" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<return xsi:type="xsd:string">
<PERSONNEL>
<INSTRUCTOR>
<PERSONNEL_ID>0001</PERSONNEL_ID>
<FIRST_NAME>Mike</FIRST_NAME>
</INSTRUCTOR>
<INSTRUCTOR>
<PERSONNEL_ID>0002</PERSONNEL_ID>
<FIRST_NAME>Joanna</FIRST_NAME>
</INSTRUCTOR>
</return>
</ns1:etaWebServicesResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我正在尝试使用此Google App脚本
function myFunction() {
var xmlInstructor = "<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:" +
'SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Body><etaws><operation opstype="instructor">export</operation><parameters><customercode>AAAAAAAA</customercode><accesscode>BBBBBBBBB</accesscode><username>xybvsfsddfas</username></parameters></etaws></SOAP-ENV:Body></SOAP-ENV:Envelope>';
var url = "https://ABCsystems.com/tseta/servlet/ABC?xmldata=" + xmlInstructor;
var urlx = encodeURI(url);
var XMLFetch = UrlFetchApp.fetch(urlx).getContentText();
var document = XmlService.parse(XMLFetch);
var documentPretty = XmlService.getPrettyFormat().format(document);
Logger.log("PrettyFormat:");
Logger.log(documentPretty);
Logger.log("Descendants:");
Logger.log(document.getDescendants());
var root = document.getRootElement();
Logger.log("getRootElement:" + root);
var instructors = root.getChild("PERSONNEL");
Logger.log("Personnel:" + instructors);
}
文档日志正确显示了XML,如顶部所示。
后代的日志具有以下信息(第一部分):
[19-05-14 15:12:09:409 PDT] Descendants:
[19-05-14 15:12:09:427 PDT] [[Element: <SOAP-ENV:Envelope [Namespace: http://schemas.xmlsoap.org/soap/envelope/]/>], [Element: <SOAP-ENV:Body [Namespace: http://schemas.xmlsoap.org/soap/envelope/]/>], [Element: <ns1:etaWebServicesResponse [Namespace: http://ws.ABCsystems.com/ABC.wsdl]/>], [Element: <return/>],
, [Element: <PERSONNEL/>],
, [Element: <INSTRUCTOR/>],
, [Element: <PERSONNEL_ID/>], 0001,
, [Element: <FIRST_NAME/>], Mike,
getRootElement的日志:
[19-05-14 15:12:09:428 PDT] getRootElement:[Element: <SOAP-ENV:Envelope [Namespace: http://schemas.xmlsoap.org/soap/envelope/]/>]
,最后讲师= null,我不会深入研究。有什么建议吗?
答案 0 :(得分:0)
PERSONNEL
的值。如果我的理解是正确的,那么该示例脚本如何?
var data = '<?xml version="1.0" encoding="UTF-8" ?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Body><ns1:etaWebServicesResponse xmlns:ns1="http://ws.ABC.com/ABCws.wsdl" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><return xsi:type="xsd:string"><PERSONNEL><INSTRUCTOR><PERSONNEL_ID>0001</PERSONNEL_ID> <FIRST_NAME>Mike</FIRST_NAME> </INSTRUCTOR><INSTRUCTOR><PERSONNEL_ID>0002</PERSONNEL_ID> <FIRST_NAME>Joanna</FIRST_NAME></INSTRUCTOR></PERSONNEL></return></ns1:etaWebServicesResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>';
var xml = XmlService.parse(data);
var rootChildren = xml.getRootElement().getChildren();
for (var i = 0; i < rootChildren.length; i++) {
if (rootChildren[i].getName() == "Body" ) {
var c1 = rootChildren[i].getChildren();
for (var j = 0; j < c1.length; j++) {
var c2 = c1[j].getChild("return").getChild("PERSONNEL").getChildren();
for (var k = 0; k < c2.length; k++) {
var PERSONNEL_ID = c2[k].getChild("PERSONNEL_ID").getValue();
var FIRST_NAME = c2[k].getChild("FIRST_NAME").getValue();
Logger.log("PERSONNEL_ID: %s, FIRST_NAME: %s", PERSONNEL_ID, FIRST_NAME)
}
}
}
}
XmlService.parse()
,我在</PERSONNEL>
之前添加了</return>
。