VBA按属性名称选择XML元素val

时间:2016-09-08 05:45:07

标签: xml excel vba excel-vba

我一直在搜索SO(以及互联网的其他部分)以获得答案,但我似乎找不到基于属性选择XML节点的解决方案。
这是我下面的XML,用于从REST服务XML

放置productcategoryid
  <lst name="responseHeader">
    <int name="status">0</int>
    <int name="QTime">0</int>
    <lst name="params">
      <str name="q">*:*</str>
      <str name="indent">true</str>
      <str name="wt">xml</str>
    </lst>
  </lst>

  <result name="response" numFound="5429" start="0">
    <doc>
      <int name="idProductCategory">2</int>
      <str name="categoryname">Live Animals</str>
      <int name="categoryLevel">2</int>
      <str name="bestOfferEnabled">false</str>
      <str name="leafCategory">true</str>
      <int name="parentCategoryId">1</int>
      <long name="_version_">1535190804282212352</long>
    </doc>
  </result>

</response>

我需要通过VBA代码获取idProductCategory的元素,即2,但我无法通过以下代码获取它。

 Sub getProductCategory(prodCatName As String)
    Dim result1 As String
    Dim result As String
    Dim myURL As String
    Dim winHttpReq As Object

    Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

    myURL = "http://localhost:8080/solr/category/select?q=" & prodCatName & "&wt=json"

    MsgBox myURL

    winHttpReq.Open "GET", myURL, False
    winHttpReq.Send

    MsgBox winHttpReq.responseText

    Dim doc_XML As DOMDocument60
    Set doc_XML = New DOMDocument60
    result = winHttpReq.responseText
    doc_XML.Load result

    Set List = doc_XML.documentElement.childNodes
    For Each sub_list In List
        If sub_list.Attributes(0).Text = "response" Then
            For Each Node In sub_list.childNodes(0).childNodes
                If Node.Attributes(0).Text = "idProductCategory" Then
                    result1 = Node.nodeTypedValue
                End If
            Next Node
        End If
    Next sub_list

End Sub

所以,请帮助我,我正在努力解决这个问题,我需要从上面的XML中获取属性名称的元素值,并将其放在Excel中的特定单元格中。

2 个答案:

答案 0 :(得分:1)

此代码有效,它不如您尝试使用的查询优雅,但IMO更容易理解,因为使用xml可能会有点混乱。

Sub prueba2()
Dim doc_XML As DOMDocument60

Set doc_XML = New DOMDocument60

data = winHttpReq.responseText
doc_XML.Load data

Set List = doc_XML.DocumentElement.ChildNodes
For Each sub_list In List
    If sub_list.Attributes(0).Text = "response" Then
        For Each Node In sub_list.ChildNodes(0).ChildNodes
            If Node.Attributes(0).Text = "idProductCategory" Then
                result = Node.nodeTypedValue
            End If
        Next Node
    End If
Next sub_list
End Sub

使用的xml示例是:

<response>
<lst name="responseHeader">
  <int name="status">0</int>
  <int name="QTime">0</int>
  <lst name="params">
    <str name="q">*:*</str>
    <str name="indent">true</str>
    <str name="wt">xml</str>
  </lst>
</lst>
<result name="response" numFound="5429" start="0">
  <doc>
    <int name="idProductCategory">2</int>
    <str name="categoryname">Live Animals</str>
    <int name="categoryLevel">2</int>
    <str name="bestOfferEnabled">false</str>
    <str name="leafCategory">true</str>
    <int name="parentCategoryId">1</int>
    <long name="_version_">1535190804282212352</long>
  </doc>
</result>
</response>

答案 1 :(得分:0)

使用SELECT LAST_CLOSING_BALANCE_DATES.ACCT_NBR, MIN(BUSINESS_DATES.DATE_VALUE) AS FIRST_BUSINESS_DAY FROM BUSINESS_DATES JOIN SD_CALENDAR ON BUSINESS_DATES.CODE = SD_CALENDAR.CODE JOIN AS_AS_RECEPTION_CONF ON AS_AS_RECEPTION_CONF.CALENDAR_ID = SD_CALENDAR.ID JOIN SD_ACCT_GRP ON AS_AS_RECEPTION_CONF.ACCT_GRP_ID = SD_ACCT_GRP.ID JOIN SD_ACCT_GRP_MEMBER ON SD_ACCT_GRP.ID = SD_ACCT_GRP_MEMBER.GRP_ID JOIN SD_ACCT ON SD_ACCT.ID = SD_ACCT_GRP_MEMBER.ACCT_ID JOIN LAST_CLOSING_BALANCE_DATES ON SD_ACCT.ACCT_NBR = LAST_CLOSING_BALANCE_DATES.ACCT_NBR WHERE BUSINESS_DATES.DATE_VALUE > LAST_CLOSING_BALANCE_DATES.BAL_DATE GROUP BY LAST_CLOSING_BALANCE_DATES.ACCT_NBR 代码可能如下所示。 HTH

SelectSingleNode