如何在 AutoItV3 中使用 MSXML 检索嵌入的 XML 值?

How do I retrieve embedded XML values using MSXML in AutoItV3?

我正在尝试使用 AutoItV3 将一些实体自动插入到一个软件中。

如果我的自动化可以从 xml 文件中读取信息并使用它来生成我的实体,那将会容易得多,因为我可以在不同的 xml 文件中解析不同的测试。

我正在使用流行的扩展程序 MSXML 来尝试执行此操作。这可以在这里找到: https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=44418

我的 XML 是一个相对简单的结构,在我所有的 'Entities'

中,每个 'Entity' 下都有不同的字段
<?xml version="1.0" encoding="UTF-8"?>
<entities>
    <entity>
        <name>
            Mation Jr, Mr Auto
        </name>
        <legalname>
            Mr Auto Mation Jr
        </legalname>
    </entity>
        <entity>
        <name>
            Mation Sr, Mr Auto
        </name>
        <legalname>
            Mr Auto Mation Sr
        </legalname>
    </entity>
</entities>

在我的脚本头中,我正在导入 MSXML au3 文件并设置 XML 路径

#include <_MSXML.au3>
; Set the XML file
$xmlpath = @ScriptDir & "\Entity.xml"

My Question is, how can I iterate through the attributes of each Entity within all Entities?

This is what I have so far, but i am not understanding how I would retrieve values from an individual entity listed under the Entities node:

; Fetch All Entities from XAML
$ENTITIES = _MSXML_SelectNodes($oXml, "entities/entity")

If ($ENTITIES[0] > 0) Then
; This part works and will iterate for x amount of entities provided 

; Fetch Entity as pos $i
For $i = 1 To $ENTITIES[0] Step 1

    ; How can I iterate through attributes from ENTITIES[$i] ??

Next
Else
    MsgBox(4096, 'Error', 'No entity was provided')
EndIf

我知道我的问题很宽泛,但我认为应该有足够的信息来开始

该 UDF 的这个问题是它似乎想要 return 字符串而不是更有用的 xml 对象。我会避免它,而只是自己将 com 对象与 $oXml = ObjCreate("Msxml2.DOMDocument") 一起使用,然后查看 documentation here.

但无论如何,我认为这段代码可以满足您的需求:

; Set the XML file
$xmlpath = @ScriptDir & "\Entity.xml"

$oXml = ObjCreate("Msxml2.DOMDocument")

$oXml.load($xmlpath)

; Fetch All Entities from XAML
$objNodeList = $oXml.selectNodes("entities/entity")
For $node in $objNodeList
    ConsoleWrite($node.nodename & @CRLF)
    $objChildNodeList = $node.selectNodes("*")
    For $ChildNode in $objChildNodeList
        ConsoleWrite(@TAB & $ChildNode.nodename & ' = ' & $ChildNode.text & @CRLF)
    Next

Next

请注意,实际上不需要使用 UDF,您只需使用 com 对象的内置方法即可。在我看来,这比使用 UDF 更简单。

另一件总体上值得一提的事情是,如果您在弄清楚如何在 autoit 中做任何事情时遇到困难,您可以尝试搜索如何在 vba 或 vbs 中做同样的事情,因为语言非常相似,autoit 可以使用 vba/vbs 中使用的所有 com 对象。当 vba/vbs 做这样的事情时 Set oXml = CreateObject("Msxml2.DOMDocument") 只需在 autoit 中这样做:$oXml = ObjCreate("Msxml2.DOMDocument").