Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA XML如何从子节点获取父属性?_Xml_Vba_Nodes - Fatal编程技术网

VBA XML如何从子节点获取父属性?

VBA XML如何从子节点获取父属性?,xml,vba,nodes,Xml,Vba,Nodes,我有一个搜索屏幕,您可以在其中选择不同的选项(作为子节点的项目)。点击搜索后,我想浏览一个XML文档并检索父节点属性。例如: <?xml version="1.0" encoding="UTF-8"?> <brands> <BrandA Name="A Brand"> <Color>Black</Color> <Thickness>1"</Thickness> <Textu

我有一个搜索屏幕,您可以在其中选择不同的选项(作为子节点的项目)。点击搜索后,我想浏览一个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> 

所以这会返回所有的东西,品牌名称,颜色,厚度,质地。我只需要品牌名,即“C品牌”

而不是
T.ParentNode.Text
我相信您将需要
T.ParentNode.Attributes.getNamedItem(“Name”).Text

它将有助于显示您的“搜索”代码
parentNode
可能是您想要的-然后
getAttribute()
@Tim Williams我尝试了parentNode,但它返回了该节点下的所有内容。IE品牌名称、颜色、厚度和质地。Tim想说的是:显示你的VBA代码。在你展示你的尝试之前,你说你已经尝试了什么并不重要,它是有效的。现在,如果在这个节点上有另一个parentnode呢。如何获得第二级parentnode?我想,对于每一级,我都必须添加一个额外的parentnode,即ParenNode.parentnode.Attributes.getNamedItem(“AnotherName”)。文本在XML DOM(以及带有HTML库的HTML DOM)中移动实际上非常有趣。例如,您可以使用Node.Parent.Parent.NextSibling.Child.Child.text在其中移动,这会将您踢向节点的祖父母,踢向节点的兄弟姐妹(节点的姑姑/叔叔),然后踢向他们的孙儿并拉取他们的文本。诀窍是使用
.getElementsByTagName
或其他元素搜索函数尽可能接近,然后使用关系缩小到您想要的范围。
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