我在使用getchild(name)方法解析XML .google appscript XMLSERVICE时总是返回null?

时间:2016-11-22 06:50:28

标签: google-apps-script xml-parsing

var resposeXML='<?xml version="1.0" encoding="UTF-8"?> \
<GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents"> \
<Timestamp>2016-11-21T13:53:57.509Z</Timestamp> \
<Ack>Success</Ack> \
<Version>991</Version> \
<Build>E991_INTL_APIXO_18185083_R1</Build> \
<PaginationResult> \
<TotalNumberOfPages>2</TotalNumberOfPages> \
<TotalNumberOfEntries>130</TotalNumberOfEntries> \
</PaginationResult> \
<HasMoreOrders>true</HasMoreOrders> \
<OrderArray> \
<Order> \
<OrderID>281928885737-1591172077018</OrderID><OrderStatus>Completed</OrderStatus><AmountPaid currencyID="INR">204.0</AmountPaid> \
</Order> \
</OrderArray> \
</GetOrdersResponse> ';


var outputDocument = XmlService.parse(resposeXML); 
var rootEle=outputDocument.getRootElement();
Logger.log(resposeXML);
Logger.log("   rootEle " + rootEle);
Logger.log("   rootEle " + rootEle.getChild('Timestamp'));
//alternately if i use index instead of name its returning the element
Logger.log("   rootEle " + rootEle.getChildren()[0]);
Logger.log("   rootEle " + rootEle.getChildren('Timestamp'));
var Timestamp=rootEle.getChild("Timestamp").getText();



Logger.log("   rootEle " + rootEle.getChild('Timestamp'));

此行返回null

交替地,如果我使用索引而不是名称它返回元素但我不需要每次为我的xml中的所有元素使用索引,因为元素的顺序可能不同,即某些元素可能会错过一些时间导致不同的顺序。例如,如果没有时间戳,则ack index为1,它将为0。

所以我需要根据元素的名称而不是元素索引来导航xml。

请帮助解决问题。

Logger.log("   rootEle " + rootEle.getChildren()[0]);

我试过以下代码返回任何内容

Logger.log("   rootEle " + rootEle.getChildren('Timestamp'));

输出看起来像这样

[16-11-21 22:38:42:923 PST] <?xml version="1.0" encoding="UTF-8"?> <GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents"> <Timestamp>2016-11-21T13:53:57.509Z</Timestamp> <Ack>Success</Ack> <Version>991</Version> <Build>E991_INTL_APIXO_18185083_R1</Build> <PaginationResult> <TotalNumberOfPages>2</TotalNumberOfPages> <TotalNumberOfEntries>130</TotalNumberOfEntries> </PaginationResult> <HasMoreOrders>true</HasMoreOrders> <OrderArray> <Order> <OrderID>281928885737-1591172077018</OrderID><OrderStatus>Completed</OrderStatus><AmountPaid currencyID="INR">204.0</AmountPaid> </Order> </OrderArray> </GetOrdersResponse> 
[16-11-21 22:38:42:923 PST]    rootEle [Element: <GetOrdersResponse [Namespace: urn:ebay:apis:eBLBaseComponents]/>]
[16-11-21 22:38:42:924 PST]    rootEle null
[16-11-21 22:38:42:927 PST]    rootEle [Element: <Timestamp [Namespace: urn:ebay:apis:eBLBaseComponents]/>]
[16-11-21 22:38:42:928 PST]    rootEle 

请帮助我在哪里做错了。这对我来说很有意思

1 个答案:

答案 0 :(得分:0)

这里的关键是使用文档中指定的命名空间:

  var resposeXML = '... your XML ...';

  var outputDocument = XmlService.parse(resposeXML); 
  var rootEle = outputDocument.getRootElement();
  // grab the namespace definition from the root element
  var ns = rootEle.getNamespace();

  Logger.log('Timestamp: ' + rootEle.getChild('Timestamp', ns).getText());
  // Timestamp: 2016-11-21T13:53:57.509Z

注意变量ns中的命名空间如何作为第二个参数传递给getChild('timestamp', ns)

希望这有帮助