VBA Excel:XML,获取特定节点
Posted
技术标签:
【中文标题】VBA Excel:XML,获取特定节点【英文标题】:VBA Excel: XML, Get certain Node 【发布时间】:2018-09-10 09:58:05 【问题描述】:我在选择 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
我很确定这只是一个小失败,但我现在尝试的时间太长了..所以如果有人能帮助我就好了..
最好的问候 卢卡
【问题讨论】:
【参考方案1】:这似乎有效。我正在从文件中读取您的 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
【讨论】:
非常感谢。这正是我一直在寻找的,一个简单的 Line 命令,可以从 xml 中获取这个该死的 id。非常感谢您的帮助,非常感谢!也很有趣知道我不必写入整个连接字符串,使其更具可读性!有一个很棒的朋友!【参考方案2】:这是我设法构建的:
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
在即时窗口中:
OK BOA_FIT1D8651D1-99E2-4D77-9BFF-1A667AA9398D
FIT
IGNORE BOA_LIMS
Lims
IGNORE BOA_MICRO_PIC
Microscope picture
我们的想法是尽可能多地使用Watches window
,从而使用属性。我从上层——//GetConfigurationItems
开始往下游走,循环遍历每一个。
【讨论】:
以上是关于VBA Excel:XML,获取特定节点的主要内容,如果未能解决你的问题,请参考以下文章