2014-09-18 101 views
1

我有一個搜索屏幕,您可以在其中選擇不同的選項(子節點項目)。點擊搜索後,我想通過一個XML文檔並檢索父節點屬性。例如:VBA XML如何從子節點獲取父屬性?

<?xml version="1.0" encoding="UTF-8"?> 
<brands> 
    <BrandA Name="A Brand"> 
    <Color>Black</Color> 
    <Thickness>1"</Thickness> 
    <Texture>Smooth</Texture> 
    </BrandA> 
    <BrandB Name="B Brand"> 
    <Color>Red</Color> 
    <Thickness>2"</Thickness> 
    <Texture>Smooth</Texture> 
    </BrandB> 
    <BrandC Name="C Brand"> 
    <Color>Green</Color> 
    <Thickness>3"</Thickness> 
    <Texture>Rough</Texture> 
    </BrandC> 
</brands> 

如果有人正在從「粗糙」紋理中搜索,我怎麼能得到BrandC名稱的父節點?

VBA代碼:

For Each T In objDom.getElementsByTagName("Texture") 

    MsgBox T.Text 'For testing to see what it returns (all 3 textures). 

If ComboBox3.Value = T.Text Then 
    'For testing: This returns all matching textures that was selected. 
    MsgBox T.ParentNode.Text 
End If 

所以這一切返回的品牌名稱,顏色,厚度,質地。我只需要品牌IE「C品牌」。

+0

這將有助於顯示你的「搜索」的代碼。 'parentNode'可能是你想要的 - 然後'getAttribute()' – 2014-09-18 19:22:54

+0

@Tim Williams我嘗試了parentNode,但是它返回了該節點下的所有內容。 IE品牌名稱,顏色,厚度和紋理。 – user770344 2014-09-18 19:38:02

+0

蒂姆想說什麼:顯示你的VBA代碼。直到你展示你已經嘗試過的東西,你所說的嘗試都沒有關係。 – Tomalak 2014-09-18 19:53:58

回答

2

而不是T.ParentNode.Text我相信你會想T.ParentNode.Attributes.getNamedItem("Name").Text

+0

這將返回類型不匹配錯誤。 – user770344 2014-09-18 20:41:04

+0

它的工作。現在如果我有另一個父節點,那該怎麼辦?我如何獲得二級父節點? – user770344 2014-09-18 21:12:08

+0

我想出來了,對於每個級別我都要添加一個額外的ParentNode,所以會是ParenNode.ParentNode.Attributes.getNamedItem(「AnotherName」)。文本 – user770344 2014-09-18 21:21:52

相關問題