Problem Parsing XML

49 views
Skip to first unread message

rypie _serv

unread,
Sep 26, 2020, 11:22:36 AM9/26/20
to Google Apps Script Community
I have the below code to Parse an XML Response but I have hit a brick wall!  The script writes only one record to the sheet and I cannot figure out why.

Can anybody see where I am going wrong and explain where and why? I have also included logger files below the Script along with an XML Response Example. 

Script:

 var itms = root.getChild('SaleRecord', NS).getChildren()
           //Logger.log(itms);    
  var values = itms.map(e => {
  var item = e.getChild('SaleRecordID', NS);
           Logger.log(item);
           
  var sku = e.getChild('CustomLabel', NS);
           Logger.log(sku);
    
  var title = e.getChild('ItemTitle', NS);
           Logger.log(title);
     
  var qty = e.getChild('QuantitySold', NS)
           Logger.log(qty);
    
  var id = e.getChild('OrderLineItemID', NS)
           Logger.log(id);
    
  return [item ? item.getText() : "", 
          sku ? sku.getText() : "", 
          title ? title.getText() : "", 
          qty ? qty.getText() : "", 
          id ? id.getText() : ""];
                            });
  Logger.log('Value Lengh:'+ values[0].length)
  sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);



Example of a response:

<?xml version="1.0" encoding="utf-8"?>
<GetSellingManagerSoldListingsResponse xmlns="urn:ebay:apis:eBLBaseComponents">
  <!-- Call-specific Output Fields -->
  <PaginationResult> PaginationResultType
    <TotalNumberOfEntries> int </TotalNumberOfEntries>
    <TotalNumberOfPages> int </TotalNumberOfPages>
  </PaginationResult>
  <SaleRecord> SellingManagerSoldOrderType
    <BuyerEmail> string </BuyerEmail>
    <BuyerID> string </BuyerID>
    <CashOnDeliveryCost currencyID="CurrencyCodeType"> AmountType (double) </CashOnDeliveryCost>
    <CreationTime> dateTime </CreationTime>
    <DaysSinceSale> int </DaysSinceSale>
    <ItemCost currencyID="CurrencyCodeType"> AmountType (double) </ItemCost>
    <OrderStatus> SellingManagerOrderStatusType
      <CheckoutStatus> CheckoutStatusCodeType </CheckoutStatus>
      <FeedbackReceived> CommentTypeCodeType </FeedbackReceived>
      <FeedbackSent> boolean </FeedbackSent>
      <IntegratedMerchantCreditCardEnabled> boolean </IntegratedMerchantCreditCardEnabled>
      <PaidStatus> SellingManagerPaidStatusCodeType </PaidStatus>
      <PaidTime> dateTime </PaidTime>
      <PaymentHoldStatus> PaymentHoldStatusCodeType </PaymentHoldStatus>
      <PaymentMethodUsed> BuyerPaymentMethodCodeType </PaymentMethodUsed>
      <ShippedStatus> SellingManagerShippedStatusCodeType </ShippedStatus>
      <ShippedTime> dateTime </ShippedTime>
      <TotalEmailsSent> int </TotalEmailsSent>
    </OrderStatus>
    <SalePrice currencyID="CurrencyCodeType"> AmountType (double) </SalePrice>
    <SaleRecordID> long </SaleRecordID>
    <SellingManagerSoldTransaction> SellingManagerSoldTransactionType
      <CharityListing> boolean </CharityListing>
      <CustomLabel> string </CustomLabel>
      <ItemID> ItemIDType (string) </ItemID>
      <ItemTitle> string </ItemTitle>
      <ListedOn> TransactionPlatformCodeType </ListedOn>
      <!-- ... more ListedOn values allowed here ... -->
      <ListingType> ListingTypeCodeType </ListingType>
      <OrderLineItemID> string </OrderLineItemID>
      <QuantitySold> int </QuantitySold>
      <Relisted> boolean </Relisted>
      <ReservePrice currencyID="CurrencyCodeType"> AmountType (double) </ReservePrice>
      <SaleRecordID> long </SaleRecordID>
      <SecondChanceOfferSent> boolean </SecondChanceOfferSent>
      <SoldOn> TransactionPlatformCodeType </SoldOn>
      <StartPrice currencyID="CurrencyCodeType"> AmountType (double) </StartPrice>
      <TransactionID> long </TransactionID>
      <Variation> VariationType </Variation>
      <WatchCount> int </WatchCount>
    </SellingManagerSoldTransaction>
    <!-- ... more SellingManagerSoldTransaction nodes allowed here ... -->
    <ShippingAddress> AddressType
      <Name> string </Name>
      <PostalCode> string </PostalCode>
    </ShippingAddress>
    <ShippingDetails> ShippingDetailsType
      <ShippingType> ShippingTypeCodeType </ShippingType>
    </ShippingDetails>
    <TotalAmount currencyID="CurrencyCodeType"> AmountType (double) </TotalAmount>
    <TotalQuantity> int </TotalQuantity>
    <UnpaidItemStatus> UnpaidItemStatusTypeCodeType </UnpaidItemStatus>
  </SaleRecord>
  <!-- ... more SaleRecord nodes allowed here ... -->
  <!-- Standard Output Fields -->
  <Ack> AckCodeType </Ack>
  <Build> string </Build>
  <CorrelationID> string </CorrelationID>
  <Errors> ErrorType
    <ErrorClassification> ErrorClassificationCodeType </ErrorClassification>
    <ErrorCode> token </ErrorCode>
    <ErrorParameters ParamID="string"> ErrorParameterType
      <Value> string </Value>
    </ErrorParameters>
    <!-- ... more ErrorParameters nodes allowed here ... -->
    <LongMessage> string </LongMessage>
    <SeverityCode> SeverityCodeType </SeverityCode>
    <ShortMessage> string </ShortMessage>
  </Errors>
  <!-- ... more Errors nodes allowed here ... -->
  <HardExpirationWarning> string </HardExpirationWarning>
  <Timestamp> dateTime </Timestamp>
  <Version> string </Version>
</GetSellingManagerSoldListingsResponse>


Logger Files:

[20-09-24 19:15:45:313 BST] Success
[20-09-24 19:15:45:315 BST] 2020-09-24T18:15:45.157Z
[20-09-24 19:15:45:323 BST] 40
[20-09-24 19:15:45:378 BST] [Element: <SaleRecordID [Namespace: urn:ebay:apis:eBLBaseComponents]/>]
[20-09-24 19:15:45:383 BST] [Element: <CustomLabel [Namespace: urn:ebay:apis:eBLBaseComponents]/>]
[20-09-24 19:15:45:386 BST] [Element: <ItemTitle [Namespace: urn:ebay:apis:eBLBaseComponents]/>]
[20-09-24 19:15:45:389 BST] [Element: <QuantitySold [Namespace: urn:ebay:apis:eBLBaseComponents]/>]
[20-09-24 19:15:45:393 BST] [Element: <OrderLineItemID [Namespace: urn:ebay:apis:eBLBaseComponents]/>]
[20-09-24 19:15:45:399 BST] null
[20-09-24 19:15:45:401 BST] null
[20-09-24 19:15:45:404 BST] null
[20-09-24 19:15:45:407 BST] null
[20-09-24 19:15:45:409 BST] null
[20-09-24 19:15:45:413 BST] null
[20-09-24 19:15:45:415 BST] null
[20-09-24 19:15:45:417 BST] null
[20-09-24 19:15:45:419 BST] null
[20-09-24 19:15:45:422 BST] null
[20-09-24 19:15:45:424 BST] null
[20-09-24 19:15:45:428 BST] null
[20-09-24 19:15:45:431 BST] null
[20-09-24 19:15:45:433 BST] null
[20-09-24 19:15:45:436 BST] null
[20-09-24 19:15:45:438 BST] null
[20-09-24 19:15:45:441 BST] null
[20-09-24 19:15:45:446 BST] null
[20-09-24 19:15:45:449 BST] null
[20-09-24 19:15:45:451 BST] null
[20-09-24 19:15:45:454 BST] null
[20-09-24 19:15:45:456 BST] null
[20-09-24 19:15:45:459 BST] null
[20-09-24 19:15:45:461 BST] null
[20-09-24 19:15:45:462 BST] null
[20-09-24 19:15:45:464 BST] null
[20-09-24 19:15:45:466 BST] null
[20-09-24 19:15:45:468 BST] null
[20-09-24 19:15:45:470 BST] null
[20-09-24 19:15:45:473 BST] null
[20-09-24 19:15:45:475 BST] null
[20-09-24 19:15:45:478 BST] null
[20-09-24 19:15:45:480 BST] null
[20-09-24 19:15:45:482 BST] null
[20-09-24 19:15:45:484 BST] null
[20-09-24 19:15:45:486 BST] null
[20-09-24 19:15:45:488 BST] null
[20-09-24 19:15:45:491 BST] null
[20-09-24 19:15:45:493 BST] null
[20-09-24 19:15:45:495 BST] null
[20-09-24 19:15:45:497 BST] null
[20-09-24 19:15:45:499 BST] null
[20-09-24 19:15:45:502 BST] null
[20-09-24 19:15:45:504 BST] null
[20-09-24 19:15:45:506 BST] null
[20-09-24 19:15:45:508 BST] null
[20-09-24 19:15:45:511 BST] null
[20-09-24 19:15:45:513 BST] null
[20-09-24 19:15:45:516 BST] null
[20-09-24 19:15:45:519 BST] null
[20-09-24 19:15:45:521 BST] null
[20-09-24 19:15:45:523 BST] null
[20-09-24 19:15:45:526 BST] null
[20-09-24 19:15:45:528 BST] null
[20-09-24 19:15:45:530 BST] null
[20-09-24 19:15:45:532 BST] null
[20-09-24 19:15:45:534 BST] null
[20-09-24 19:15:45:537 BST] null
[20-09-24 19:15:45:540 BST] null
[20-09-24 19:15:45:542 BST] null


bbau...@gmail.com

unread,
Sep 26, 2020, 5:46:54 PM9/26/20
to Google Apps Script Community
The script code shown does not show how you iterate over the records. How does the transition to the next record go?

суббота, 26 сентября 2020 г. в 18:22:36 UTC+3, rypie _serv:

rypie _serv

unread,
Sep 27, 2020, 11:45:24 AM9/27/20
to google-apps-sc...@googlegroups.com
Hi @ cy66oTa

It produces an array then sends it to the sheet. The part which sends it to the sheets is here:


sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);



Pedro Teixeira

unread,
Sep 27, 2020, 12:12:14 PM9/27/20
to google-apps-sc...@googlegroups.com
Hi, 

How is structured each element of the array? 

In my humble opinion you should replace the map function by a for loop because it would be easier to debug the state of the array prior to send it to the sheet.

Pedro

Em Dom, 27 de set de 2020 12:45, rypie _serv <rypie...@gmail.com> escreveu:
Hi @ cy66oTa

It oroduces and array then sends it to the sheet. The part which sends it to the sheets is 

rypie _serv

unread,
Sep 27, 2020, 12:55:32 PM9/27/20
to google-apps-sc...@googlegroups.com
I have changed my code as below script and also included the new log from that script. I may end up trying the loop as Pedro suggested but I really wanted to stay away from a loop because its much slower than the array method.

Script:

  var itms = root.getChild('SaleRecord', NS).getChild('SellingManagerSoldTransaction', NS).getChildren();
   
Logger.log(root.getChild('SaleRecord', NS).getChild('SellingManagerSoldTransaction', NS).getChild('OrderLineItemID', NS).getText());
   
Logger.log(root.getChild('SaleRecord', NS).getChildren().length)

 
var values = itms.map(e => {
 
var item = e.getChild('SaleRecordID', NS);

           
Logger.log('Item ' + item);

           
 
var sku = e.getChild('CustomLabel', NS);

           
Logger.log('sku ' + sku);

   
 
var title = e.getChild('ItemTitle', NS);

           
Logger.log('title '+title);
     
 
var qty = e.getChild('QuantitySold', NS);
           
Logger.log('qty '+qty);
   
 
var id = e.getChild('OrderLineItemID', NS);
           
Logger.log('id '+id);

   
 
return [item ? item.getText() : "",
          sku
? sku.getText() : "",
          title
? title.getText() : "",
          qty
? qty.getText() : "",
          id
? id.getText() : ""];
                           
});

 
 
//Logger.log('Value Lengh:'+ values[0].length)

  sheet
.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);

Logger:

[20-09-27 09:48:06:916 PDT] Success
[20-09-27 09:48:06:919 PDT] 2020-09-27T16:48:06.818Z
[20-09-27 09:48:06:924 PDT] Page# 1
[20-09-27 09:48:07:058 PDT] Number of Orders 78
[20-09-27 09:48:07:069 PDT] 160989605461-2048236393006
[20-09-27 09:48:07:081 PDT] 13.0
[20-09-27 09:48:07:084 PDT] Item null
[20-09-27 09:48:07:086 PDT] sku null
[20-09-27 09:48:07:089 PDT] title null
[20-09-27 09:48:07:091 PDT] qty null
[20-09-27 09:48:07:094 PDT] id null
[20-09-27 09:48:07:096 PDT] Item null
[20-09-27 09:48:07:099 PDT] sku null
[20-09-27 09:48:07:101 PDT] title null
[20-09-27 09:48:07:103 PDT] qty null
[20-09-27 09:48:07:105 PDT] id null
[20-09-27 09:48:07:107 PDT] Item null
[20-09-27 09:48:07:109 PDT] sku null
[20-09-27 09:48:07:111 PDT] title null
[20-09-27 09:48:07:114 PDT] qty null
[20-09-27 09:48:07:116 PDT] id null
[20-09-27 09:48:07:118 PDT] Item null
[20-09-27 09:48:07:120 PDT] sku null
[20-09-27 09:48:07:122 PDT] title null
[20-09-27 09:48:07:124 PDT] qty null
[20-09-27 09:48:07:126 PDT] id null
[20-09-27 09:48:07:128 PDT] Item null
[20-09-27 09:48:07:130 PDT] sku null
[20-09-27 09:48:07:132 PDT] title null
[20-09-27 09:48:07:134 PDT] qty null
[20-09-27 09:48:07:136 PDT] id null
[20-09-27 09:48:07:139 PDT] Item null
[20-09-27 09:48:07:141 PDT] sku null
[20-09-27 09:48:07:143 PDT] title null
[20-09-27 09:48:07:145 PDT] qty null
[20-09-27 09:48:07:147 PDT] id null
[20-09-27 09:48:07:149 PDT] Item null
[20-09-27 09:48:07:151 PDT] sku null
[20-09-27 09:48:07:153 PDT] title null
[20-09-27 09:48:07:155 PDT] qty null
[20-09-27 09:48:07:157 PDT] id null
[20-09-27 09:48:07:158 PDT] Item null
[20-09-27 09:48:07:160 PDT] sku null
[20-09-27 09:48:07:162 PDT] title null
[20-09-27 09:48:07:164 PDT] qty null
[20-09-27 09:48:07:166 PDT] id null
[20-09-27 09:48:07:167 PDT] Item null
[20-09-27 09:48:07:169 PDT] sku null
[20-09-27 09:48:07:171 PDT] title null
[20-09-27 09:48:07:172 PDT] qty null
[20-09-27 09:48:07:173 PDT] id null
[20-09-27 09:48:07:175 PDT] Item null
[20-09-27 09:48:07:176 PDT] sku null
[20-09-27 09:48:07:178 PDT] title null
[20-09-27 09:48:07:179 PDT] qty null
[20-09-27 09:48:07:181 PDT] id null
[20-09-27 09:48:07:182 PDT] Item null
[20-09-27 09:48:07:184 PDT] sku null
[20-09-27 09:48:07:186 PDT] title null
[20-09-27 09:48:07:187 PDT] qty null
[20-09-27 09:48:07:189 PDT] id null
[20-09-27 09:48:07:191 PDT] Item null
[20-09-27 09:48:07:193 PDT] sku null
[20-09-27 09:48:07:195 PDT] title null
[20-09-27 09:48:07:197 PDT] qty null
[20-09-27 09:48:07:199 PDT] id null
[20-09-27 09:48:07:201 PDT] Item null
[20-09-27 09:48:07:203 PDT] sku null
[20-09-27 09:48:07:205 PDT] title null
[20-09-27 09:48:07:207 PDT] qty null
[20-09-27 09:48:07:209 PDT] id null





On Sunday, 27 September 2020 17:12:14 UTC+1, Pedro Teixeira wrote:
Hi, 

How is structured each element of the array? 

In my humble opinion you should replace the map function by a for loop because it would be easier to debug the state of the array prior to send it to the sheet.

Pedro

Reply all
Reply to author
Forward
0 new messages