我敢肯定这是一个愚蠢的问题(我是googlescript的新手),但是经过大量的搜索并在墙上碰了几个小时后,我非常感谢您的帮助,向我展示了如何编写googlescript来检索以下xml提要中“ celular”和“ mensagem”的内容:
<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="https://www.twwwireless.com.br/reluzcap/wsreluzcap">
<xs:schema id="OutDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="OutDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="SMSMO">
<xs:complexType>
<xs:sequence>
<xs:element name="seunum" type="xs:string" minOccurs="0" />
<xs:element name="celular" type="xs:string" minOccurs="0" />
<xs:element name="mensagem" type="xs:string" minOccurs="0" />
<xs:element name="status" type="xs:string" minOccurs="0" />
<xs:element name="datarec" type="xs:dateTime" minOccurs="0" />
<xs:element name="dataenv" type="xs:dateTime" minOccurs="0" />
<xs:element name="datastatus" type="xs:dateTime" minOccurs="0" />
<xs:element name="op" type="xs:short" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<OutDataSet xmlns="">
<SMSMO diffgr:id="SMSMO1" msdata:rowOrder="0">
<seunum>MSG </seunum>
<celular>5511984326293 </celular>
<mensagem>2</mensagem>
<status>MO</status>
<datarec>2018-08-04T11:58:11.877-03:00</datarec>
<dataenv>2018-08-04T11:58:12.45-03:00</dataenv>
<datastatus>2018-08-04T11:58:12.45-03:00</datastatus>
<op>2</op>
</SMSMO>
</OutDataSet>
</diffgr:diffgram>
</DataSet>
如果我运行以下脚本
var document = XmlService.parse(bggXml);
var root = document.getRootElement();
var entries = new Array();
entries = root.getChildren('OutDataSet');
Logger.log(entries);
日志返回
[18-08-05 14:40:23:594 BRT] []
如果我运行以下脚本
var document = XmlService.parse(bggXml);
var root = document.getRootElement();
var entries = new Array();
entries = root.getAllContent();
Logger.log(entries);
日志返回
[18-08-05 14:46:41:593 BRT] [[元素:http://www.w3.org/2001/XMLSchema] />],[元素:]]
如果我运行以下脚本
var document = XmlService.parse(bggXml);
var root = document.getRootElement();
var entries = new Array();
entries = root.getChildren();
Logger.log(entries);
日志与上一个相同
[18-08-05 14:52:13:581 BRT] [[元素:http://www.w3.org/2001/XMLSchema] />],[元素:]]
答案 0 :(得分:1)
利用Apps脚本的XmlService来解析文档。该类具有一些函数,可用于深入查询文档中的适当元素并提取所需的信息。
答案 1 :(得分:0)
在您的脚本中,这些修改和示例如何?我认为您的情况有几个答案。因此,请考虑其中的两个。
使用XmlService时,可以按如下所示修改脚本。
var bggXml = '<?xml version="1.0" encoding="utf-8"?><DataSet xmlns="https://www.twwwireless.com.br/reluzcap/wsreluzcap"> <xs:schema id="OutDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="OutDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="SMSMO"> <xs:complexType> <xs:sequence> <xs:element name="seunum" type="xs:string" minOccurs="0" /> <xs:element name="celular" type="xs:string" minOccurs="0" /> <xs:element name="mensagem" type="xs:string" minOccurs="0" /> <xs:element name="status" type="xs:string" minOccurs="0" /> <xs:element name="datarec" type="xs:dateTime" minOccurs="0" /> <xs:element name="dataenv" type="xs:dateTime" minOccurs="0" /> <xs:element name="datastatus" type="xs:dateTime" minOccurs="0" /> <xs:element name="op" type="xs:short" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> <OutDataSet xmlns=""> <SMSMO diffgr:id="SMSMO1" msdata:rowOrder="0"> <seunum>MSG </seunum> <celular>5511984326293 </celular> <mensagem>2</mensagem> <status>MO</status> <datarec>2018-08-04T11:58:11.877-03:00</datarec> <dataenv>2018-08-04T11:58:12.45-03:00</dataenv> <datastatus>2018-08-04T11:58:12.45-03:00</datastatus> <op>2</op> </SMSMO> </OutDataSet> </diffgr:diffgram></DataSet>';
var document = XmlService.parse(bggXml);
var rootChildren = document.getRootElement().getChildren();
var res = {};
rootChildren.forEach(function(e) {
if (e.getName() == "diffgram") {
var c = e.getChild("OutDataSet").getChild("SMSMO").getChildren();
c.forEach(function(f) {
res[f.getName()] = f.getValue().trim()
});
}
});
Logger.log(res.celular); // 5511984326293
Logger.log(res.mensagem); // 2
使用正则表达式时,可以使用以下示例脚本。 bggXml
与上面相同。
var res1 = bggXml.match(/<celular>([\d ]+)<\/celular>/i)[1].trim();
var res2 = bggXml.match(/<mensagem>([\d ]+)<\/mensagem>/i)[1].trim();
Logger.log(res1); // 5511984326293
Logger.log(res2); // 2
如果这些不是您想要的,对不起。