gpt4 book ai didi

excel - VBA Excel : Loop through XML Attributes

转载 作者:行者123 更新时间:2023-12-04 20:30:09 26 4
gpt4 key购买 nike

我实际上是在 excel 中构建一个程序来将数据从 web 服务填充到 word 文档中。
因为 XML 是以一种奇怪的方式构建的,所以我必须遍历节点的属性来创建一个列表。从这个列表中,我将创建一个表。

我的问题是这个循环,我将向您展示代码:

    ConnectionString = "//GetConfigurationItems/ConfigurationItem/AttachmentTypes/AttachmentType"
tblFilter = "1D8651D1-99E2-4D77-9BFF-1A667AA9398D"
id = "14"

Set valuesOXML = CreateObject("msxml2.DOMDocument.4.0")
valuesOXML.LoadXML DMIService.execute(Webservice, functionName, portName, "<![CDATA[<GetConfigurationItems><ConfigurationItem ID=""" & ID & """ Filter=""" & tblFilter & """ Deleted=""0""/></GetConfigurationItems>]]>")

Set fourthNameField = valuesOXML.SelectNodes(ConnectionString)
For Each ftfield In fourthNameField
werte = werte & ftfield.Attributes(0).Text & ";"

Dim x As Integer
For x = 0 To ftfield.Attributes.Item - 1
Debug.Print ftfield.Attributes.Item(x)
Next x
Next ftfield

保存 werte 变量的命令正在运行。但是通过属性的循环失败并显示失败文本:

"Object doens't support this property or method".



XML 如下所示:
<?xml version="1.0"?>
<GetConfigurationItems Error="False">
<ConfigurationItem ID="14" Filter="1D8651D1-99E2-4D77-9BFF-1A667AA9398D" Deleted="0">
<AttachmentTypes DropDownType="14" Filter="1D8651D1-99E2-4D77-9BFF-1A667AA9398D" Deleted="0">
<AttachmentType ShortDesc="BOA_FIT" VersionNo="2" ID="1D8651D1-99E2-4D77-9BFF-1A667AA9398D">FIT</AttachmentType>
</AttachmentTypes>
</ConfigurationItem>
</GetConfigurationItems>

最佳答案

你没有做以下等效的事情吗?用您的连接代码替换我从文件中加载的内容。

Option Explicit
Public Sub testing()
Dim xmlDoc As New MSXML2.DOMDocument60, mainNode As Object, Node As Object, child As Object, child2 As Object
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.Load "C:\Users\User\Desktop\Test.xml"
Set mainNode = xmlDoc.SelectNodes("//GetConfigurationItems")

For Each Node In mainNode
For Each child In Node.ChildNodes
Debug.Print child.BaseName & " :" & child.Text
For Each child2 In child.ChildNodes
Debug.Print child2.BaseName & " : " & child2.Text
Next
Next
Next Node
End Sub

带属性:
Option Explicit
Public Sub testing()
Dim xmlDoc As New MSXML2.DOMDocument60, mainNode As Object, Node As Object, child As Object, child2 As Object, i As Long
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.Load "C:\Users\User\Desktop\Test.xml"
Set mainNode = xmlDoc.SelectNodes("//GetConfigurationItems")

For Each Node In mainNode
For Each child In Node.ChildNodes
If child.Attributes.Length > 0 Then
For i = 0 To child.Attributes.Length - 1
Debug.Print child.BaseName & " : " & child.Attributes(i).nodeName & " - " & child.Attributes(i).NodeValue; ""
Next
End If
For Each child2 In child.ChildNodes
If child2.Attributes.Length > 0 Then
For i = 0 To child2.Attributes.Length - 1
Debug.Print child2.BaseName & " : " & child2.Attributes(i).nodeName & " - " & child2.Attributes(i).NodeValue
Next
End If
Next
Next
Next Node
End Sub

关于excel - VBA Excel : Loop through XML Attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52221640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com