VBA Excel: XML, 获取某个节点

VBA Excel: XML, Get certain Node

我在选择 xml 中的某些节点时遇到问题。 XML 看起来像这样:

<?xml version="1.0"?>
<GetConfigurationItems Error="False">
    <ConfigurationItem ID="14" Deleted="0">
        <AttachmentTypes DropDownType="14" Filter="%" Deleted="0">
            <AttachmentType ShortDesc="BOA_FIT" VersionNo="2" ID="1D8651D1-99E2-4D77-9BFF-1A667AA9398D">FIT</AttachmentType>
            <AttachmentType ShortDesc="BOA_LIMS" VersionNo="3" ID="F543938A-693F-457A-97AA-010065D0BA4E">Lims</AttachmentType>
            <AttachmentType ShortDesc="BOA_MICRO_PIC" VersionNo="1" ID="CC3FB18D-1E3F-400A-AD52-971A78A5517D">Microscope picture</AttachmentType>
        </AttachmentTypes>
    </ConfigurationItem>
</GetConfigurationItems>

现在我想保存 ID 属性,但是从某个值开始,让我们只取 FIT。我尝试了很多变化,我真的不知道我做错了什么.. :( 使用此代码,我从 Web 服务获得 XML:

Webservice = "http://xxx.xxx.xxx/mm/rm/webservice/RMWS_ConfigurationRead.asmx?wsdl"

    functionName = "GetConfigurationItems"
    portName = "RMWS_ConfigurationReadSoap"
    Set DMIService = New DMIService
    Set oXML = CreateObject("msxml2.DOMDocument.6.0")
    oXML.LoadXML DMIService.execute(Webservice, functionName, portName, "<![CDATA[<GetConfigurationItems><ConfigurationItem ID=""" & ID & """ Deleted=""0""/></GetConfigurationItems>]]>")

下面是连接字符串的一些尝试(不同的尝试是 bsp1):

    //GetConfigurationItems/ConfigurationItem[@ID="14"]/AttachmentTypes/AttachmentType[text="FIT"]/@ID
    //GetConfigurationItems/ConfigurationItem[@ID="14"]/AttachmentTypes[AttachmentType="FIT"]/@ID
    //GetConfigurationItems/ConfigurationItem[@ID="14"]/AttachmentTypes/[AttachmentType="FIT"]/@ID
    //GetConfigurationItems/ConfigurationItem[@ID="14"]/AttachmentTypes[AttachmentType[@Name="FIT"]/@ID
    //GetConfigurationItems/ConfigurationItem[@ID="14"]/AttachmentTypes[AttachmentType="FIT"]/@ID

ID = oXML.SelectSingleNode(bsp1).Text

我很确定这只是一个小失败,但我现在尝试的时间太长了..如果有人能帮助我就太好了..

此致 卢卡

这似乎有效。我正在从文件中读取您的 XML 示例。

Option Explicit
Public Sub GetNode()
    Dim xmlDoc      As MSXML2.DOMDocument60
    Set xmlDoc = New MSXML2.DOMDocument60
    xmlDoc.async = False
    xmlDoc.validateOnParse = True

    If Not xmlDoc.Load("C:\Users\User\Desktop\Testing.xml") Then
        MsgBox "Problem"
        Exit Sub
    End If
    Debug.Print xmlDoc.SelectSingleNode("//AttachmentType[text()='FIT']").Attributes.getNamedItem("ID").Text
End Sub

或者

Debug.Print xmlDoc.SelectSingleNode("//*[text()='FIT']").Attributes.getNamedItem("ID").Text

这是我成功构建的:

Option Explicit
Sub TestMe()

    Dim xmlObj As Object
    Set xmlObj = CreateObject("MSXML2.DOMDocument")

    xmlObj.async = False
    xmlObj.validateOnParse = False
    xmlObj.Load (ThisWorkbook.Path & "\someXML.xml")

    Dim nodesThatMatter As Object
    Dim node            As Object
    Set nodesThatMatter = xmlObj.SelectNodes("//GetConfigurationItems")

    Dim level1 As Object
    Dim level2 As Object
    Dim level3 As Object
    Dim level4 As Object

    For Each level1 In nodesThatMatter
        For Each level2 In level1.ChildNodes
            For Each level3 In level2.ChildNodes
                For Each level4 In level3.ChildNodes
                    With level4
                        If .Attributes(0).Value Like "*FIT*" Then
                            Debug.Print "OK " & .Attributes(0).Value & .Attributes(2).Value
                        Else
                            Debug.Print "IGNORE " & .Attributes(0).Value
                        End If
                        Debug.Print .Text & vbCrLf
                    End With
                Next level4
            Next level3
        Next level2
    Next level1

End Sub

立即有了这个window:

OK BOA_FIT1D8651D1-99E2-4D77-9BFF-1A667AA9398D
FIT

IGNORE BOA_LIMS
Lims

IGNORE BOA_MICRO_PIC
Microscope picture

我们的想法是尽可能多地使用 Watches window,从而使用属性。我从上层开始 - //GetConfigurationItems 到下游,遍历每一层。