我一直在搜索SO(以及Internet的其余部分)来寻找答案,但是我似乎找不到用于基于属性选择XML节点的解决方案。
这是我的XML,下面是用于productcategoryid
从REST服务XML放置的XML
<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>
我需要的元素idProductCategory
,即2
,通过VBA代码,但我无法从下面的代码做到这一点。
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中的特定单元格中。
这段代码有效,比您尝试使用的查询不太优雅,但是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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句