使用VBA(Excel)通过XML循环

时间:2015-10-27 15:22:11

标签: xml excel-vba vba excel

我是XML String的完全新手,我希望有人可以指出我正确的方向。我包含一个示例XML和我正在尝试使用的代码。

我能够检索我的XML字符串并循环遍历它,但是我必须遗漏一些东西,这就是我想要发生的事情, 从订单节点开始 循环遍历此节点并返回BaseName和Text,当它到达Shipping Address时,我需要遍历此Node并在此子节点下获取结果? (BaseName和Text)但返回第一个循环并继续获取其余信息。

非常感谢有人提供的任何帮助,谢谢Mike

以下是XML示例:

<?xml version="1.0"?>
<ListOrdersResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
  <ListOrdersResult>
    <Orders>
      <Order>
        <LatestShipDate>2015-10-29T06:59:59Z</LatestShipDate>
        <OrderType>StandardOrder</OrderType>
        <PurchaseDate>2015-10-25T04:04:33Z</PurchaseDate>
        <BuyerEmail>y73fy@marketplace.amazon.com</BuyerEmail>
        <AmazonOrderId>258-7431234-5680250</AmazonOrderId>
        <LastUpdateDate>2015-10-26T14:35:02Z</LastUpdateDate>
        <NumberOfItemsShipped>8</NumberOfItemsShipped>
        <ShipServiceLevel>Std Cont US Street Addr</ShipServiceLevel>
        <OrderStatus>Shipped</OrderStatus>
        <SalesChannel>Amazon.com</SalesChannel>
        <ShippedByAmazonTFM>false</ShippedByAmazonTFM>
        <IsBusinessOrder>false</IsBusinessOrder>
        <LatestDeliveryDate>2015-11-05T07:59:59Z</LatestDeliveryDate>
        <NumberOfItemsUnshipped>0</NumberOfItemsUnshipped>
        <BuyerName>JOHN</BuyerName>
        <EarliestDeliveryDate>2015-10-30T07:00:00Z</EarliestDeliveryDate>
        <OrderTotal>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>7.35</Amount>
        </OrderTotal>
        <IsPremiumOrder>false</IsPremiumOrder>
        <EarliestShipDate>2015-10-27T07:00:00Z</EarliestShipDate>
        <MarketplaceId>ATVPD212X0DER</MarketplaceId>
        <FulfillmentChannel>MFN</FulfillmentChannel>
        <PaymentMethod>Other</PaymentMethod>
        <ShippingAddress>
          <StateOrRegion>NJ</StateOrRegion>
          <City>NORTH BERGEN</City>
          <Phone>5551234444</Phone>
          <CountryCode>US</CountryCode>
          <PostalCode>07047-7206</PostalCode>
          <Name>John DD</Name>
          <AddressLine1>8100 TOMS RD APT 33</AddressLine1>
        </ShippingAddress>
        <IsPrime>false</IsPrime>
        <ShipmentServiceLevelCategory>Standard</ShipmentServiceLevelCategory>
        <SellerOrderId>12345677</SellerOrderId>
      </Order>
      <Order>
        <LatestShipDate>2015-10-29T06:59:59Z</LatestShipDate>
        <OrderType>StandardOrder</OrderType>
        <PurchaseDate>2015-10-25T05:05:14Z</PurchaseDate>
        <BuyerEmail>12345@marketplace.amazon.com</BuyerEmail>
        <AmazonOrderId>123456789-7092245</AmazonOrderId>
        <LastUpdateDate>2015-10-26T14:35:02Z</LastUpdateDate>
        <NumberOfItemsShipped>3</NumberOfItemsShipped>
        <ShipServiceLevel>Std Cont US Street Addr</ShipServiceLevel>
        <OrderStatus>Shipped</OrderStatus>
        <SalesChannel>Amazon.com</SalesChannel>
        <ShippedByAmazonTFM>false</ShippedByAmazonTFM>
        <IsBusinessOrder>false</IsBusinessOrder>
        <LatestDeliveryDate>2015-11-05T07:59:59Z</LatestDeliveryDate>
        <NumberOfItemsUnshipped>0</NumberOfItemsUnshipped>
        <BuyerName>Can Ss</BuyerName>
        <EarliestDeliveryDate>2015-10-30T07:00:00Z</EarliestDeliveryDate>
        <OrderTotal>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>3.35</Amount>
        </OrderTotal>
        <IsPremiumOrder>false</IsPremiumOrder>
        <EarliestShipDate>2015-10-27T07:00:00Z</EarliestShipDate>
        <MarketplaceId>ATVPD212X0DER</MarketplaceId>
        <FulfillmentChannel>MFN</FulfillmentChannel>
        <PaymentMethod>Other</PaymentMethod>
        <ShippingAddress>
          <StateOrRegion>PA</StateOrRegion>
          <City>KING OF PRUSSIA</City>
          <Phone>5552221234</Phone>
          <CountryCode>US</CountryCode>
          <PostalCode>19406-2462</PostalCode>
          <Name>Duo Xu</Name>
          <AddressLine1>251 DEB ST APT 222</AddressLine1>
        </ShippingAddress>
        <IsPrime>false</IsPrime>
        <ShipmentServiceLevelCategory>Standard</ShipmentServiceLevelCategory>
        <SellerOrderId>12345678</SellerOrderId>
      </Order>
    </Orders>
    <CreatedBefore>2015-10-26T15:12:55Z</CreatedBefore>
  </ListOrdersResult>
  <ResponseMetadata>
    <RequestId>123c1234-b12b-1c23-1234-1f23de00b123</RequestId>
  </ResponseMetadata>

以下是我正在尝试使用的代码:

Set list = xdoc.SelectNodes("//Order")
Set list2 = xdoc.SelectNodes("//ShippingAddress")
X = 1
Sheets(4).Cells(X, 1) = STRHTML

For Each node In list
If (node.HasChildNodes) Then
    For Each childNode In node.ChildNodes
        'Debug.Print childNode.BaseName & " " & childNode.Text
        With Sheets(2)
            If childNode.BaseName = "ShippingAddress" Then
                Stop
                For Each node2 In list2
                        Stop

                            If (node2.HasChildNodes) Then
                                For Each childNode2 In node2.ChildNodes
                                    .Cells(X, 3) = childNode2.BaseName: .Cells(X, 4) = childNode2.Text
                                    X = X + 1
                                Next
                            End If
                            Exit For
            Next
            Else
                .Cells(X, 1) = childNode.BaseName: .Cells(X, 2) = childNode.Text
            End If

            X = X + 1


        End With
    Next childNode
End If
Next node

0 个答案:

没有答案