vba - 查找节点的 xpath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7486129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:04:23  来源:igfitidea点击:

vba - finding the xpath of a node

xmlexcelvbaxpath

提问by toop

c.Value is a text from a cell (typical values are: name, type, weight, currency but could be anything) in the first row of the sheet.

c.Value 是来自工作表第一行单元格的文本(典型值是:名称、类型、重量、货币,但可以是任何值)。

filenames is a loop from Dir() basically looping thru XMLs in a folder.

文件名是来自 Dir() 的循环,基本上通过文件夹中的 XML 循环。

These xmls may have the lowest level node 'weight' (for example) but under multiple different parent nodes. Ie these 3 different xpaths: //Client/personal/weight and //Client/details/weight and //Client/details/info/weight.

这些 xml 可能具有最低级别的节点“权重”(例如),但位于多个不同的父节点之下。即这 3 个不同的 xpath://Client/personal/weight 和 //Client/details/weight 和 //Client/details/info/weight。

I've got this code:

我有这个代码:

   Dim aDoc As DOMDocument
    Dim aNode As IXMLDOMNode
    Set aDoc = LoadXmlDoc(filenames(f))
    Set aNodes = aDoc.getElementsByTagName(c.Value)
For f = 1 To UBound(filenames)
    If aNodes.Length > 0 Then 'if at least one node is present
       For Each aNode In aNodes 'loop thru each occurence of a node
          c.Offset(f, 0).Value = aNode.Text & "parent is " & aNode.parentNode.parentNode
       Next
    End If
Nxt f

What I'm trying to do is get a distinct list of the xpaths for the child node I specify in c.value.

我想要做的是获取我在 c.value 中指定的子节点的 xpath 的不同列表。

For example,

例如,

if c.value is weight

I'm trying to get this list back:

我正在尝试取回此列表:

//Client/personal/weight
//Client/details/weight
//Client/details/info/weight.

http://msdn.microsoft.com/en-us/library/aa163921(office.10).aspxis a good reference but I can't seem to find how to get the xpath from a child node. I just managed to get the immediate parent node.

http://msdn.microsoft.com/en-us/library/aa163921(office.10).aspx是一个很好的参考,但我似乎无法找到如何从子节点获取 xpath。我刚刚设法获得了直接的父节点。

回答by Tim Williams

You need to start with the node and walk up its parents until you reach the document level.

您需要从节点开始并向上走,直到到达文档级别。

Sub Tester()

    Dim oDoc As New MSXML2.DOMDocument
    Dim oNodes As MSXML2.IXMLDOMNodeList
    Dim oNode As MSXML2.IXMLDOMNode
    Dim pNode As MSXML2.IXMLDOMNode
    Dim XML As String, sPath As String

    XML = "<Client><LastName>Bill</LastName><FirstName>Gates</FirstName>" & _
    "<MiddleName/><Suffix/><DateOfBirth>30-May-1968</DateOfBirth>" & _
    "<PlaceOfBirth/><SSN>n/a</SSN><Gender>Male</Gender><District>" & _
    "<City>SHELTON</City><Mayor>wong</Mayor></District><State>WA</State>" & _
    "<Zip>96484</Zip></Client>"

    oDoc.LoadXML XML

    Set oNodes = oDoc.getElementsByTagName("City")
    If oNodes.Length > 0 Then

        For Each oNode In oNodes
            sPath = oNode.nodeName
            Set pNode = Nothing
            Do
                If pNode Is Nothing Then
                    Set pNode = oNode.ParentNode
                Else
                    Set pNode = pNode.ParentNode
                End If

                If pNode.nodeTypeString <> "document" Then
                    sPath = pNode.nodeName & "/" & sPath
                Else
                    sPath = "//" & sPath
                    Exit Do
                End If
            Loop
            Debug.Print sPath
        Next oNode
    End If

End Sub