VBA Excel:遍历XML属性

时间:2018-09-07 11:29:11

标签: excel xml vba

我实际上是在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变量的命令正在运行。但是通过属性的循环会失败,并显示failure-Text:

  

“对象不支持此属性或方法”。

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>

2 个答案:

答案 0 :(得分:1)

您是否不执行以下等效操作?用您的连接代码替换我从文件中加载的文件。

<?php
exec('my_batch.bat',$result,$exitcode);
echo $result[0];
echo '<br />';
echo $exitcode;

具有属性:

D:\tools\xampp\htdocs\test>my_batch.bat
quitting

D:\tools\xampp\htdocs\test>echo %errorlevel%
12

D:\tools\xampp\htdocs\test>

答案 1 :(得分:0)

尝试此操作(在使用XML作为字符串的VBA中工作)

  

我添加了MSXML2库作为参考(工具>参考> Microsoft XML,v6.0)**

Dim valuesOXML As MSXML2.DOMDocument60 

Dim ConnectionString As String
Dim fourthNameField As IXMLDOMNodeList
Dim ftfield As IXMLDOMNode
Dim werte As String

ConnectionString = "//GetConfigurationItems/ConfigurationItem/AttachmentTypes/AttachmentType"

Set valuesOXML = CreateObject("Msxml2.DOMDocument.6.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.Length - 1
         Debug.Print ftfield.Attributes.Item(x).Text
    Next x
Next ftfield
End Sub