autoitmsxmlmsxml3

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


I am trying to use AutoItV3 to automate the insertion of some Entities into a piece of software.

It will be far easier if my automation can read information from an xml file and use this to generate my entities, as I can then parse in different xml files for different tests.

I am using a popular extension MSXML to try and do this. This can be found here: https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=44418

My XML is a relatively simply structure where I will have various fields under each 'Entity' within all of my 'Entities'

<?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>

In my script header I am importing the MSXML au3 file and setting the XML path

#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

I understand my question is quite broad but I think there should be enough information to start with


Solution

  • This issue with that UDF is that it seems to want to return strings for everything instead of xml objects which are more useful. I would avoid it and instead just just use the com object yourself with $oXml = ObjCreate("Msxml2.DOMDocument") and then have a look at the documentation here.

    But anyways, I think this code will get you what you want:

    ; 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
    

    Notice how there is really no need to use a UDF and you can just use the com object's built in methods. In my opinion, this is simpler than using the UDF.

    Another thing in general that is worth mentioning is that if you ever have trouble figuring out how to do anything in autoit you can try searching for how to do that same thing in vba or vbs since the languages are pretty similar and autoit can use all the com objects that are used in vba/vbs. When vba/vbs does something like this Set oXml = CreateObject("Msxml2.DOMDocument") just do this in autoit: $oXml = ObjCreate("Msxml2.DOMDocument").