[英]VBA Excel: XML, Get certain Node
I have a Problem with selecting certain nodes in a xml. 我在选择xml中的某些节点时遇到问题。 The XML looks like this:
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>
Now i want to save the ID Attribute, BUT from a certain value, for the beginning lets just take FIT. 现在,我想从某个值保存ID属性,但从一开始就开始进行FIT。 I tried so many variations, i really dont know what im doing wrong.. :( With this Code i get XML from the Webservice:
我尝试了许多变化,我真的不知道我在做什么错.. :(通过此代码,我从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>]]>")
So here are some of the try's of the connection string(the different trys are bsp1): 所以这是连接字符串的一些尝试(不同的尝试是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
Im quite sure thats its just a small failure, but im now trying for too long.. So would be nice if someone can help me out.. 我很确定那只是一个小小的失败,但是我现在尝试了太久了。如果有人可以帮助我,那将很好。
Best regards luca 最好的问候卢卡
This seems to work. 这似乎有效。 I am reading your XML sample in from a file.
我正在从文件中读取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
Or 要么
Debug.Print xmlDoc.SelectSingleNode("//*[text()='FIT']").Attributes.getNamedItem("ID").Text
This is what I have managed to build: 这是我设法建立的:
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
With this in the immediate window: 在立即窗口中:
OK BOA_FIT1D8651D1-99E2-4D77-9BFF-1A667AA9398D
FIT
IGNORE BOA_LIMS
Lims
IGNORE BOA_MICRO_PIC
Microscope picture
The idea is to use the Watches window
as much as you can and thus use the attributes. 想法是尽可能多地使用“
Watches window
,从而使用属性。 I have started from the upper level - //GetConfigurationItems
and went downstream, looping through each one. 我从上层开始-
//GetConfigurationItems
,然后向下走,遍历每一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.