使用VBA计算子节点

时间:2016-09-18 09:59:29

标签: xml vba parsing dom

我想要一些XML,我想要计算它包含的信息的产品数量......但是努力获得正确的语法,这是我的代码......

Dim XMLHttpRequest As XMLHTTP60
Set XMLHttpRequest = New MSXML2.XMLHTTP60

Dim objxmlSKU As MSXML2.IXMLDOMNodeList
Dim SKUCount As MSXML2.IXMLDOMNodeList
Dim objxmldoc As New MSXML2.DOMDocument60
Dim xmlNode As MSXML2.IXMLDOMNodeList
Dim count As Integer

XMLHttpRequest.Open "GET", signedURL, False
XMLHttpRequest.send (signedURL)
objxmldoc.loadXML (XMLHttpRequest.responseXML.XML)

xmlNamespaces =    "xmlns:ns1='https://mws.amazonservices.com/FulfillmentInventory/2010-10-01'"
objxmldoc.SetProperty "SelectionNamespaces", xmlNamespaces

Set SKUCount = objxmldoc.selectNodes("//ns1:InventorySupplyList/ns1:member") 

count = 0
For Each member In SKUCount
    count = count + 1
Next
Debug.Print count

&安培;这里是示例XML(我希望计算以成员为首的块数...因此在下面的代码中有两个带有XML名称成员的块..

<ListInventorySupplyResponse xmlns="http://mws.amazonaws.com/FulfillmentInventory/2010-10-01/">
    <ListInventorySupplyResult>
        <MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
        <InventorySupplyList>
            <member>
                <Condition>NewItem</Condition>
                <SupplyDetail/>
                <TotalSupplyQuantity>43</TotalSupplyQuantity>
                <EarliestAvailability>
                    <TimepointType>Immediately</TimepointType>
                </EarliestAvailability>
                <FNSKU>B005H38BZ4</FNSKU>
                <InStockSupplyQuantity>43</InStockSupplyQuantity>
                <ASIN>B005H38BZ4</ASIN>
                <SellerSKU>dtp-11 fba</SellerSKU>
            </member>
            <member>
                <Condition>NewItem</Condition>
                <SupplyDetail/>
                <TotalSupplyQuantity>40</TotalSupplyQuantity>
                <EarliestAvailability>
                    <TimepointType>Immediately</TimepointType>
                </EarliestAvailability>
                <FNSKU>B01BvMUHM8</FNSKU>
                <InStockSupplyQuantity>40</InStockSupplyQuantity>
                <ASIN>B01BVMUHM8</ASIN>
                <SellerSKU>dsx-90 fba</SellerSKU>
            </member>
        </InventorySupplyList>
    </ListInventorySupplyResult>
    <ResponseMetadata>
        <RequestId>8a7d1832-b271-4171-991f-7e39feee5bf1</RequestId>
    </ResponseMetadata>
</ListInventorySupplyResponse>

2 个答案:

答案 0 :(得分:1)

有关如何使用XML doc执行此操作的示例:

Public Sub Test_XML()


    Dim SKUCount As MSXML2.IXMLDOMNodeList
    Dim objxmldoc As New MSXML2.DOMDocument60
    Dim count As Integer

    Dim strXML As String
    Dim node As MSXML2.IXMLDOMNode


    strXML = strXML & "<ListInventorySupplyResponse>  "
    strXML = strXML & "    <ListInventorySupplyResult>                                                                  "
    strXML = strXML & "        <InventorySupplyList>                                                                    "
    strXML = strXML & "            <member>                                                                             "
    strXML = strXML & "                <Condition>NewItem</Condition>                                                   "
    strXML = strXML & "                <SupplyDetail/>                                                                  "
    strXML = strXML & "                <TotalSupplyQuantity>43</TotalSupplyQuantity>                                    "
    strXML = strXML & "                <EarliestAvailability>                                                           "
    strXML = strXML & "                    <TimepointType>Immediately</TimepointType>                                   "
    strXML = strXML & "                </EarliestAvailability>                                                          "
    strXML = strXML & "                <FNSKU>B005H38BZ4</FNSKU>                                                        "
    strXML = strXML & "                <InStockSupplyQuantity>43</InStockSupplyQuantity>                                "
    strXML = strXML & "                <ASIN>B005H38BZ4</ASIN>                                                          "
    strXML = strXML & "                <SellerSKU>dtp-11 fba</SellerSKU>                                                "
    strXML = strXML & "            </member>                                                                            "
    strXML = strXML & "            <member>                                                                             "
    strXML = strXML & "                <Condition>NewItem</Condition>                                                   "
    strXML = strXML & "                <SupplyDetail/>                                                                  "
    strXML = strXML & "                <TotalSupplyQuantity>40</TotalSupplyQuantity>                                    "
    strXML = strXML & "                <EarliestAvailability>                                                           "
    strXML = strXML & "                    <TimepointType>Immediately</TimepointType>                                   "
    strXML = strXML & "                </EarliestAvailability>                                                          "
    strXML = strXML & "                <FNSKU>B01BvMUHM8</FNSKU>                                                        "
    strXML = strXML & "                <InStockSupplyQuantity>40</InStockSupplyQuantity>                                "
    strXML = strXML & "                <ASIN>B01BVMUHM8</ASIN>                                                          "
    strXML = strXML & "                <SellerSKU>dsx-90 fba</SellerSKU>                                                "
    strXML = strXML & "            </member>                                                                            "
    strXML = strXML & "        </InventorySupplyList>                                                                   "
    strXML = strXML & "    </ListInventorySupplyResult>                                                                 "
    strXML = strXML & "    <ResponseMetadata>                                                                           "
    strXML = strXML & "        <RequestId>8a7d1832-b271-4171-991f-7e39feee5bf1</RequestId>                              "
    strXML = strXML & "    </ResponseMetadata>                                                                          "
    strXML = strXML & "</ListInventorySupplyResponse>                                                                   "
    strXML = strXML & ""


    objxmldoc.loadXML (strXML)


    Set SKUCount = objxmldoc.selectNodes("//ListInventorySupplyResponse/ListInventorySupplyResult/InventorySupplyList/member")

    ' Get the number of "member" nodes:
    Debug.Print "Total member nodes : " & SKUCount.length

    count = 0
    For Each node In SKUCount
        ' Loop on "Member" nodes
        count = count + 1
    Next
    Debug.Print "Loop count = " & count

End Sub

输出:

Total member nodes : 2
Loop count = 2

答案 1 :(得分:0)

使用selectNodes时,您需要在文档中指定带有节点名称的XPath,因此没有ns1:前缀:

 Set SKUCount = objxmldoc.selectNodes("//InventorySupplyList/member")

然后循环将起作用并返回2的计数。

如果您想在没有循环的情况下获取计数,可以使用length属性:

Debug.Print SKUCount.length