我在选择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>]]>")
所以这里是连接字符串的一些尝试(不同的trys是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
在即时窗口中显示:
OK BOA_FIT1D8651D1-99E2-4D77-9BFF-1A667AA9398D
FIT
IGNORE BOA_LIMS
Lims
IGNORE BOA_MICRO_PIC
Microscope picture
我们的想法是尽可能多地使用Watches window
,从而使用属性。我从上层开始 - //GetConfigurationItems
并向下游,循环遍历每一个。