我正在尝试将XML响应中的数据导入Excel工作表,并且我在单个父节点下重复子节点时遇到困难。我需要能够为每个类别导入CategoryID,CategoryName和CategoryParentName行。我已经能够得到前两个,但不能找出自重复以来的CategoryParentName。 XML如下。
<GetSuggestedCategoriesResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2010-01-19T22:08:02.568Z</Timestamp>
<Ack>Success</Ack>
<Version>647</Version>
<Build>E647_CORE_BUNDLED_10438248_R1</Build>
<SuggestedCategoryArray>
<SuggestedCategory>
<Category>
<CategoryID>18871</CategoryID>
<CategoryName>Memory Cards</CategoryName>
<CategoryParentID>625</CategoryParentID>
<CategoryParentID>3327</CategoryParentID>
<CategoryParentID>18866</CategoryParentID>
<CategoryParentName>Cameras & Photo</CategoryParentName>
<CategoryParentName>Camera Accessories</CategoryParentName>
<CategoryParentName>Digital Camera Accessories</CategoryParentName>
</Category>
<PercentItemFound>4</PercentItemFound>
</SuggestedCategory>
<SuggestedCategory>
<Category>
<CategoryID>48629</CategoryID>
<CategoryName>Color</CategoryName>
<CategoryParentID>293</CategoryParentID>
<CategoryParentID>14948</CategoryParentID>
<CategoryParentID>48633</CategoryParentID>
<CategoryParentID>48638</CategoryParentID>
<CategoryParentID>48628</CategoryParentID>
<CategoryParentName>Electronics</CategoryParentName>
<CategoryParentName>Gadgets</CategoryParentName>
<CategoryParentName>Surveillance</CategoryParentName>
<CategoryParentName>Surveillance Cameras</CategoryParentName>
<CategoryParentName>Wired Cameras</CategoryParentName>
</Category>
<PercentItemFound>4</PercentItemFound>
</SuggestedCategory>
这是我对VBA代码的尝试。非常感谢任何帮助!
Sub CategoryResponse()
Outputrow = Sheet1.Range("E1") + 7
Nodes = "GetSuggestedCategoriesResponse/SuggestedCategoryArray/SuggestedCategory"
For Each CAT In responseItem.SelectNodes(Nodes)
Outputcol = 3
Sheet1.Range("A" & Outputrow).Value = CAT.SelectSingleNode("PercentItemFound").Text
Sheet1.Range("B" & Outputrow).Value = CAT.SelectSingleNode("Category/CategoryID").Text
Nodes1 = "GetSuggestedCategoriesResponse/SuggestedCategoryArray/SuggestedCategory/CategoryParentName"
For Each PAR In responseItem.SelectNodes(Nodes1)
Sheet1.Cells(Outputrow, Outputcol).Value = PAR.SelectSingleNode("CategoryParentName").Text
Outputcol = Outputcol + 1
Next PAR
Sheet1.Cells(Outputrow, Outputcol).Value = UST.SelectSingleNode("Category/CategoryName").Text
Outputrow = Outputrow + 1
Next UST
End Sub
答案 0 :(得分:1)
Nodes1
循环中的当前节点创建 For Each CAT ...
,而不是相对于responseItem
。尝试:
Nodes1 = "Category/CategoryParentName"
For Each PAR In CAT.SelectNodes(Nodes1)
Sheet1.Cells(Outputrow, Outputcol).Value = PAR.Text
Outputcol = Outputcol + 1
Next PAR
您想要选择与当前SuggestedCategory节点相关的CategoryParentName节点。您的原始代码将返回所有CategoryParentNames(与selectNodes表达式匹配),无论它们与哪个SuggestedCategory节点相关