1
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'));

this line returning null

alternately if i use index instead of name its returning the element but i dont need to use index every time for all element in my xml as the order of element may differ i.e some element may miss some time result in different order. e.g ack index is 1 if timestamp is not there it will be 0.

so i need to navigate the xml based on name of the element not the index of element .

please help sort out the issue.

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

i tried this below code returns nothing

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

The output looks like this

[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 

Please help me where i am doing wrong.this has becom nighmare for me

1 Answer 1

3

The key here is to use the namespace specified in the document:

  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

Note how the namespace, in variable ns is passed as a second argument to getChild('timestamp', ns).

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.