title: How do I sort this XML parsing problem using Google Script?
body: <p>I have derived the below code from help received from @Tanaike on another project.  The code is failing on <code>var userid = e.getChild('BuyerID', NS);</code> and <code>var price = e.getChild('TotalAmount', NS)</code>. I think but not 100% sure its something to do with those elements are not in the child list here: <code>root.getChild('SaleRecord', NS).getChildren()</code></p>
  <p>I have also added the XML response below the google script code.  In addition to the above problem you will see the top SaleRecord in the XML response is for an order that has multiple line items so I need a way of handling this as well which is another headache of mine.</p>
  <p><strong>Google Script:</strong></p>
  <pre><code>  var itms = root.getChild('SaleRecord', NS).getChildren();
             Logger.log(itms);    
    var values = itms.map(e =&gt; {
    var item = e.getChild('SaleRecordID', NS);
             Logger.log(item);
    var userid = e.getChild('BuyerID', NS); //NOT Working Logger Returns Null
             Logger.log(userid);    
    var sku = e.getChild('CustomLabel', NS);
             Logger.log(sku);
      
    var title = e.getChild('ItemTitle', NS);
             Logger.log(title);
      
    var price = e.getChild('TotalAmount', NS) //NOT Working Logger Return Null
             Logger.log(price);
      
    var qty = e.getChild('QuantitySold', NS)
             Logger.log(qty);
  
          return;  ////// Good Up to here
      
    var imgurl = e.getChild('PictureDetails', NS).getChild('GalleryURL', NS);
    var qty = e.getChild('Quantity', NS);
  
    return [item ? item.getText(): &quot;&quot;, 
            userid ? userid.getText() : &quot;&quot;,  
            qty ? qty.getText() : &quot;&quot;, 
            sku ? sku.getText() : &quot;&quot;, 
            title ? title.getText() : &quot;&quot;, 
            price ? price.getText() : &quot;&quot;, 
            qty ? qty.getText() : &quot;&quot;];
                              });
    sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
    // Logger Error: TypeError: Cannot read property 'length' of undefined
    // at COPYGetSellingManagerSoldListings(Copy SoldListings:119:68)
  </code></pre>
  <p><strong>XML Response:</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;GetSellingManagerSoldListingsResponse xmlns=&quot;urn:ebay:apis:eBLBaseComponents&quot;&gt;
      &lt;Timestamp&gt;2020-09-20T14:18:59.126Z&lt;/Timestamp&gt;
      &lt;Ack&gt;Success&lt;/Ack&gt;
      &lt;Version&gt;1163&lt;/Version&gt;
      &lt;Build&gt;E1163_CORE_APISELLING_19187371_R1&lt;/Build&gt;
      &lt;SaleRecord&gt;
          &lt;SellingManagerSoldTransaction&gt;
              &lt;TransactionID&gt;############&lt;/TransactionID&gt;
              &lt;SaleRecordID&gt;252841&lt;/SaleRecordID&gt;
              &lt;ItemID&gt;###########&lt;/ItemID&gt;
              &lt;QuantitySold&gt;1&lt;/QuantitySold&gt;
              &lt;ItemTitle&gt;**?*?*?**?*?*?*?*?*&lt;/ItemTitle&gt;
              &lt;ListingType&gt;StoresFixedPrice&lt;/ListingType&gt;
              &lt;Relisted&gt;false&lt;/Relisted&gt;
              &lt;SecondChanceOfferSent&gt;false&lt;/SecondChanceOfferSent&gt;
              &lt;CustomLabel&gt;*?*?*?*?&lt;/CustomLabel&gt;
              &lt;SoldOn&gt;eBay&lt;/SoldOn&gt;
              &lt;ListedOn&gt;eBay&lt;/ListedOn&gt;
              &lt;CharityListing&gt;false&lt;/CharityListing&gt;
              &lt;OrderLineItemID&gt;160935488259-2045570273006&lt;/OrderLineItemID&gt;
          &lt;/SellingManagerSoldTransaction&gt;
          &lt;SellingManagerSoldTransaction&gt;
              &lt;TransactionID&gt;#########&lt;/TransactionID&gt;
              &lt;SaleRecordID&gt;252842&lt;/SaleRecordID&gt;
              &lt;ItemID&gt;#########&lt;/ItemID&gt;
              &lt;QuantitySold&gt;1&lt;/QuantitySold&gt;
              &lt;ItemTitle&gt;*?*?*?*?*?**?*?*?*?*?*?&lt;/ItemTitle&gt;
              &lt;ListingType&gt;StoresFixedPrice&lt;/ListingType&gt;
              &lt;Relisted&gt;false&lt;/Relisted&gt;
              &lt;SecondChanceOfferSent&gt;false&lt;/SecondChanceOfferSent&gt;
              &lt;CustomLabel&gt;*?*??**?*&lt;/CustomLabel&gt;
              &lt;SoldOn&gt;eBay&lt;/SoldOn&gt;
              &lt;ListedOn&gt;eBay&lt;/ListedOn&gt;
              &lt;CharityListing&gt;false&lt;/CharityListing&gt;
              &lt;OrderLineItemID&gt;164342889307-2045570271006&lt;/OrderLineItemID&gt;
          &lt;/SellingManagerSoldTransaction&gt;
          &lt;ShippingAddress&gt;
              &lt;Name&gt;???????&lt;/Name&gt;
              &lt;PostalCode&gt;????????&lt;/PostalCode&gt;
          &lt;/ShippingAddress&gt;
          &lt;ShippingDetails&gt;
              &lt;ShippingType&gt;NotSpecified&lt;/ShippingType&gt;
          &lt;/ShippingDetails&gt;
          &lt;TotalAmount currencyID=&quot;GBP&quot;&gt;25.49&lt;/TotalAmount&gt;
          &lt;TotalQuantity&gt;2&lt;/TotalQuantity&gt;
          &lt;ActualShippingCost currencyID=&quot;GBP&quot;&gt;0.0&lt;/ActualShippingCost&gt;
          &lt;OrderStatus&gt;
              &lt;CheckoutStatus&gt;CheckoutComplete&lt;/CheckoutStatus&gt;
              &lt;PaidStatus&gt;Paid&lt;/PaidStatus&gt;
              &lt;ShippedStatus&gt;Unshipped&lt;/ShippedStatus&gt;
              &lt;PaymentMethodUsed&gt;IntegratedMerchantCreditCard&lt;/PaymentMethodUsed&gt;
              &lt;FeedbackSent&gt;false&lt;/FeedbackSent&gt;
              &lt;TotalEmailsSent&gt;0&lt;/TotalEmailsSent&gt;
              &lt;PaidTime&gt;2020-09-20T14:17:29.000Z&lt;/PaidTime&gt;
          &lt;/OrderStatus&gt;
          &lt;SalePrice currencyID=&quot;GBP&quot;&gt;25.49&lt;/SalePrice&gt;
          &lt;DaysSinceSale&gt;0&lt;/DaysSinceSale&gt;
          &lt;BuyerID&gt;?**?*?*?*?**?*?*?*&lt;/BuyerID&gt;
          &lt;BuyerEmail&gt;?**?*?**?*?@hotmail.com&lt;/BuyerEmail&gt;
          &lt;SaleRecordID&gt;252843&lt;/SaleRecordID&gt;
          &lt;CreationTime&gt;2020-09-20T14:17:29.000Z&lt;/CreationTime&gt;
      &lt;/SaleRecord&gt;
      &lt;SaleRecord&gt;
          &lt;SellingManagerSoldTransaction&gt;
              &lt;TransactionID&gt;?**?*?*?*?*&lt;/TransactionID&gt;
              &lt;SaleRecordID&gt;252840&lt;/SaleRecordID&gt;
              &lt;ItemID&gt;?**?*?*?*?*&lt;/ItemID&gt;
              &lt;QuantitySold&gt;1&lt;/QuantitySold&gt;
              &lt;ItemTitle&gt;?*?**?*?*?*?**?*&lt;/ItemTitle&gt;
              &lt;ListingType&gt;StoresFixedPrice&lt;/ListingType&gt;
              &lt;Relisted&gt;false&lt;/Relisted&gt;
              &lt;SecondChanceOfferSent&gt;false&lt;/SecondChanceOfferSent&gt;
              &lt;CustomLabel&gt;kdpab090&lt;/CustomLabel&gt;
              &lt;SoldOn&gt;eBay&lt;/SoldOn&gt;
              &lt;ListedOn&gt;eBay&lt;/ListedOn&gt;
              &lt;CharityListing&gt;false&lt;/CharityListing&gt;
              &lt;OrderLineItemID&gt;171376693488-2207818951007&lt;/OrderLineItemID&gt;
          &lt;/SellingManagerSoldTransaction&gt;
          &lt;ShippingAddress&gt;
              &lt;Name&gt;*?*?*?**?&lt;/Name&gt;
              &lt;PostalCode&gt;?**?*?*?&lt;/PostalCode&gt;
          &lt;/ShippingAddress&gt;
          &lt;ShippingDetails&gt;
              &lt;ShippingType&gt;Flat&lt;/ShippingType&gt;
          &lt;/ShippingDetails&gt;
          &lt;TotalAmount currencyID=&quot;GBP&quot;&gt;7.99&lt;/TotalAmount&gt;
          &lt;TotalQuantity&gt;1&lt;/TotalQuantity&gt;
          &lt;ActualShippingCost currencyID=&quot;GBP&quot;&gt;0.0&lt;/ActualShippingCost&gt;
          &lt;OrderStatus&gt;
              &lt;CheckoutStatus&gt;CheckoutComplete&lt;/CheckoutStatus&gt;
              &lt;PaidStatus&gt;Paid&lt;/PaidStatus&gt;
              &lt;ShippedStatus&gt;Unshipped&lt;/ShippedStatus&gt;
              &lt;PaymentMethodUsed&gt;IntegratedMerchantCreditCard&lt;/PaymentMethodUsed&gt;
              &lt;FeedbackSent&gt;false&lt;/FeedbackSent&gt;
              &lt;TotalEmailsSent&gt;0&lt;/TotalEmailsSent&gt;
              &lt;PaidTime&gt;2020-09-20T14:10:51.000Z&lt;/PaidTime&gt;
          &lt;/OrderStatus&gt;
          &lt;SalePrice currencyID=&quot;GBP&quot;&gt;7.99&lt;/SalePrice&gt;
          &lt;DaysSinceSale&gt;0&lt;/DaysSinceSale&gt;
          &lt;BuyerID&gt;*?*?*?**?*?&lt;/BuyerID&gt;
          &lt;BuyerEmail&gt;*?*?*?*?**?*@ntlworld.com&lt;/BuyerEmail&gt;
          &lt;SaleRecordID&gt;252840&lt;/SaleRecordID&gt;
          &lt;CreationTime&gt;2020-09-20T14:04:01.000Z&lt;/CreationTime&gt;
      &lt;/SaleRecord&gt;
      &lt;SaleRecord&gt;
          &lt;SellingManagerSoldTransaction&gt;
              &lt;TransactionID&gt;?**??**?*?*?*?&lt;/TransactionID&gt;
              &lt;SaleRecordID&gt;252839&lt;/SaleRecordID&gt;
              &lt;ItemID&gt;*?*?*?*?**??*&lt;/ItemID&gt;
              &lt;QuantitySold&gt;1&lt;/QuantitySold&gt;
              &lt;ItemTitle&gt;*?*?*?*?*?*?*&lt;/ItemTitle&gt;
              &lt;ListingType&gt;StoresFixedPrice&lt;/ListingType&gt;
              &lt;Relisted&gt;false&lt;/Relisted&gt;
              &lt;SecondChanceOfferSent&gt;false&lt;/SecondChanceOfferSent&gt;
              &lt;CustomLabel&gt;*?*?**?*?*&lt;/CustomLabel&gt;
              &lt;SoldOn&gt;eBay&lt;/SoldOn&gt;
              &lt;ListedOn&gt;eBay&lt;/ListedOn&gt;
              &lt;CharityListing&gt;false&lt;/CharityListing&gt;
              &lt;OrderLineItemID&gt;160935488259-2045564148006&lt;/OrderLineItemID&gt;
          &lt;/SellingManagerSoldTransaction&gt;
          &lt;ShippingAddress&gt;
              &lt;Name&gt;*?*?*?*?*?*?*&lt;/Name&gt;
              &lt;PostalCode&gt;*?*?*?*?**?&lt;/PostalCode&gt;
          &lt;/ShippingAddress&gt;
          &lt;ShippingDetails&gt;
              &lt;ShippingType&gt;Flat&lt;/ShippingType&gt;
          &lt;/ShippingDetails&gt;
          &lt;TotalAmount currencyID=&quot;GBP&quot;&gt;8.99&lt;/TotalAmount&gt;
          &lt;TotalQuantity&gt;1&lt;/TotalQuantity&gt;
          &lt;ActualShippingCost currencyID=&quot;GBP&quot;&gt;0.0&lt;/ActualShippingCost&gt;
          &lt;OrderStatus&gt;
              &lt;CheckoutStatus&gt;CheckoutComplete&lt;/CheckoutStatus&gt;
              &lt;PaidStatus&gt;Paid&lt;/PaidStatus&gt;
              &lt;ShippedStatus&gt;Unshipped&lt;/ShippedStatus&gt;
              &lt;PaymentMethodUsed&gt;IntegratedMerchantCreditCard&lt;/PaymentMethodUsed&gt;
              &lt;FeedbackSent&gt;false&lt;/FeedbackSent&gt;
              &lt;TotalEmailsSent&gt;0&lt;/TotalEmailsSent&gt;
              &lt;PaidTime&gt;2020-09-20T13:57:15.000Z&lt;/PaidTime&gt;
          &lt;/OrderStatus&gt;
          &lt;SalePrice currencyID=&quot;GBP&quot;&gt;8.99&lt;/SalePrice&gt;
          &lt;DaysSinceSale&gt;0&lt;/DaysSinceSale&gt;
          &lt;BuyerID&gt;*?*?*?**?&lt;/BuyerID&gt;
          &lt;BuyerEmail&gt;*?*?*?*?*@hotmail.co.uk&lt;/BuyerEmail&gt;
          &lt;SaleRecordID&gt;252839&lt;/SaleRecordID&gt;
          &lt;CreationTime&gt;2020-09-20T13:57:15.000Z&lt;/CreationTime&gt;
      &lt;/SaleRecord&gt;
  </code></pre>
  <p><strong>So now I have changed it to the below:</strong></p>
  <p>It seems to run OK but it only adds one row of data to the sheet. Below this code is the Logger Text. It looks like the loop to write the data to the array is not finding the text. What am I dong wrong?</p>
  <pre><code>  var pgsnum = root.getChild('PaginationResult', NS).getChild('TotalNumberOfPages', NS).getValue();
      pgsnum = +pgsnum; // Changes String/Text to a Number
      //Logger.log(pgsnum);
  
    var totlistings = root.getChild('PaginationResult', NS).getChild('TotalNumberOfEntries', NS).getValue();
    Logger.log(totlistings);
  
    var itms = root.getChild('SaleRecord', NS).getChildren()
             //Logger.log(itms);    
    var values = itms.map(e =&gt; {
    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(): &quot;&quot;, 
            sku ? sku.getText() : &quot;&quot;, 
            title ? title.getText() : &quot;&quot;, 
            qty ? qty.getText() : &quot;&quot;, 
            id ? id.getText() : &quot;&quot;,];
                              });
  
    sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
  </code></pre>
  <p><strong>Logger Text</strong></p>
  <pre><code>[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: &lt;SaleRecordID [Namespace: urn:ebay:apis:eBLBaseComponents]/&gt;]
  [20-09-24 19:15:45:383 BST] [Element: &lt;CustomLabel [Namespace: urn:ebay:apis:eBLBaseComponents]/&gt;]
  [20-09-24 19:15:45:386 BST] [Element: &lt;ItemTitle [Namespace: urn:ebay:apis:eBLBaseComponents]/&gt;]
  [20-09-24 19:15:45:389 BST] [Element: &lt;QuantitySold [Namespace: urn:ebay:apis:eBLBaseComponents]/&gt;]
  [20-09-24 19:15:45:393 BST] [Element: &lt;OrderLineItemID [Namespace: urn:ebay:apis:eBLBaseComponents]/&gt;]
  [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
  </code></pre>