Jump to content
jswilcox1980

Read XML Node and Display in MsgBox

Recommended Posts

OK I have created a small utility that will display the file versions of the software my customers are using.  This information is displayed in a MSGBOX and saved into a Text file on the desktop.  I would like to capture the Dongle Number From the XML document, display it in my MSGBOX and save it to the text file also.  I have looked at about every xml read post there is and i am seriously missing something. 

<?xml version="1.0" encoding="UTF-8"?>

-<TServerConfig version="1">


-<Object type="TServerConfig" name="MainObject">

<!-- URL of remote web service. Leave undefined for production deployment -->


<!-- Port that the server listnes on -->


<Property name="ServerPort" value="27027"/>

<!-- Server IP, leave empty for autodetect -->


<Property name="ServerIP" value=""/>

<!-- Write more events to the log file -->


<Property name="ExtendedLogging" value="False"/>

</Object>


-<Dongles>

<Dongle Order="0" Number="0123456789"/>

<Dongle Order="1" Number="1122334455"/>

</Dongles>

</TServerConfig>

 

 

This is my code for the small utility that works , i would like to build in the Dongle numbers that could be extracted from the XML.

#include <MsgBoxConstants.au3>
#include<file.au3>

FileVersions()
Func FileVersions()
    ; Retrieve the file versions of specified 3shape application files

    $DSver = FileGetVersion("C:\Program Files\3Shape\DentalManager\Dentalmanager.exe")
    $DSSver = FileGetVersion("C:\Program Files (x86)\3Shape\Dongle Server Service\DongleServer.exe")
    $ScnSvr = FileGetVersion("C:\Program Files\3Shape\Scansuite\Scanserver.exe")
    $SID = FileGetVersion("C:\Program Files\3Shape\ScanitDental\DentalDesktop.exe")
    $AppDsgnr = FileGetVersion("C:\Program Files\3Shape\ApplianceDesigner\ApplianceDesigner.exe")
    $Splintdsgnr = FileGetVersion("C:\Program Files\3Shape\SplintDesigner Splint\ApplianceDesigner.exe")

    $Versiontextfile = (@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt')

;Clears file from previous contents
        $hFile = FileOpen($Versiontextfile, 2)
        FileClose($hFile)


; Display the file version. This should be equal to @AutoItVersion.
    MsgBox($MB_SYSTEMMODAL,'3Shape Software Versions',  'Dental System:   ' & $DSver    &@CRLF& 'Dongle Server Service:   '& $DSSver & @CRLF & 'ScanSuite:   '& $ScnSvr & @CRLF & 'ScanitDental:   '& $SID & @CRLF & 'Appliance Designer:   '& $AppDsgnr& @CRLF & 'Splint Designer:   '& $SplintDsgnr)
    MsgBox($MB_SYSTEMMODAL,'3Shape Software Versions', '       A summary has been saved on the desktop: ' & @CRLF & @CRLF& '" ' & @computername & ' 3shape Dental System File Versions.txt "')
;Below writes all data to text file on desktop
    FileWriteLine(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt',"")
    FileWriteLine(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt',"")
    FileWriteLine(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','***** 3SHAPE DENTAL SYSTEM FILE VERSIONS *****')
    FileWriteLine(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt',"")
    FileWriteLine(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt',"")
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','Dental System - '& $DSver)
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','Dongle Server Service - '& $DSSver)
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','ScanSuite - '& $ScnSvr)
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','Scanit Dental - '& $SID)
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','Appliance Designer - '& $AppDsgnr)
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','Splint Designer - '& $SplintDsgnr)

Run("notepad.exe " & $Versiontextfile, @WindowsDir, @SW_MAXIMIZE)
EndFunc

 

Share this post


Link to post
Share on other sites
#include <Constants.au3>
#include <Array.au3>

Opt ("MustDeclareVars", 1)

Local $aNumber = StringRegExp (FileRead ("File.xml"),'Dongle Order="\d+" Number="(\d+)',$STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay ($aNumber)

Try this...

Share this post


Link to post
Share on other sites

Or if you want to play with the XML UDF, you can do something like this:

 

#include <Constants.au3>
#include <Array.au3>
#include <MyIncludes\XML\xml.au3>   ; <== Change to your location

Const $gsXmlData = _
    '<?xml version="1.0" encoding="UTF-8"?>' & @CRLF & _
    '<TServerConfig version="1">' & @CRLF & _
    '    <Object type="TServerConfig" name="MainObject">' & @CRLF & _
    '        <!-- URL of remote web service. Leave undefined for production deployment -->' & @CRLF & _
    '        <!-- Port that the server listnes on -->' & @CRLF & _
    '        <Property name="ServerPort" value="27027"/>' & @CRLF & _
    '        <!-- Server IP, leave empty for autodetect -->' & @CRLF & _
    '        <Property name="ServerIP" value=""/>' & @CRLF & _
    '        <!-- Write more events to the log file -->' & @CRLF & _
    '        <Property name="ExtendedLogging" value="False"/>' & @CRLF & _
    '    </Object>' & @CRLF & _
    '    <Dongles>' & @CRLF & _
    '        <Dongle Order="0" Number="0123456789"/>' & @CRLF & _
    '        <Dongle Order="1" Number="1122334455"/>' & @CRLF & _
    '    </Dongles>' & @CRLF & _
    '</TServerConfig>' & @CRLF


show_dongle_numbers($gsXmlData)

Func show_dongle_numbers($sXML)
    Local $oXml
    Local $aResult


    $oXml = _XML_CreateDOMDocument()

    $oXml = _XML_LoadXml($oXml, $sXML)
    If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "_XML_LoadXml failed with @error = " & @error)

    $aResult = _XML_GetValue($oXml, "//Dongle/@Number")
    If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "_XML_GetValue failed with @error = " & @error)

    _ArrayDisplay($aResult, "Dongle Numbers")
EndFunc

 

Edited by TheXman

Share this post


Link to post
Share on other sites
1 hour ago, Nine said:
#include <Constants.au3>
#include <Array.au3>

Opt ("MustDeclareVars", 1)

Local $aNumber = StringRegExp (FileRead ("File.xml"),'Dongle Order="\d+" Number="(\d+)',$STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay ($aNumber)

Try this...

Nine, I like this approach but i am unable to get to display the dongle number listed when i place the $anumber variable into my msgbox display code it returns a "1" only?  How do i get it to display the 10 digit dongle number?

 

38 minutes ago, TheXman said:

Or if you want to play with the XML UDF, you can do something like this:

 

#include <Constants.au3>
#include <Array.au3>
#include <MyIncludes\XML\xml.au3>   ; <== Change to your location

Const $gsXmlData = _
    '<?xml version="1.0" encoding="UTF-8"?>' & @CRLF & _
    '<TServerConfig version="1">' & @CRLF & _
    '    <Object type="TServerConfig" name="MainObject">' & @CRLF & _
    '        <!-- URL of remote web service. Leave undefined for production deployment -->' & @CRLF & _
    '        <!-- Port that the server listnes on -->' & @CRLF & _
    '        <Property name="ServerPort" value="27027"/>' & @CRLF & _
    '        <!-- Server IP, leave empty for autodetect -->' & @CRLF & _
    '        <Property name="ServerIP" value=""/>' & @CRLF & _
    '        <!-- Write more events to the log file -->' & @CRLF & _
    '        <Property name="ExtendedLogging" value="False"/>' & @CRLF & _
    '    </Object>' & @CRLF & _
    '    <Dongles>' & @CRLF & _
    '        <Dongle Order="0" Number="0123456789"/>' & @CRLF & _
    '        <Dongle Order="1" Number="1122334455"/>' & @CRLF & _
    '    </Dongles>' & @CRLF & _
    '</TServerConfig>' & @CRLF


show_dongle_numbers($gsXmlData)

Func show_dongle_numbers($sXML)
    Local $oXml
    Local $aResult


    $oXml = _XML_CreateDOMDocument()

    $oXml = _XML_LoadXml($oXml, $sXML)
    If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "_XML_LoadXml failed with @error = " & @error)

    $aResult = _XML_GetValue($oXml, "//Dongle/@Number")
    If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "_XML_GetValue failed with @error = " & @error)

    _ArrayDisplay($aResult, "Dongle Numbers")
EndFunc

 

TheXman, I am not sure about the XML portion of this code above, is this specified for structure only or as the source of the code to work?  I was unclear what i state on the original post.  The dongle numbers will be different on each PC this script will be ran on.

Share this post


Link to post
Share on other sites
11 minutes ago, jswilcox1980 said:

TheXman, I am not sure about the XML portion of this code above, is this specified for structure only or as the source of the code to work?  I was unclear what i state on the original post.  The dongle numbers will be different on each PC this script will be ran on.

The example that I provided was merely a guide.  It was provided to show you how to pull just the values of the number attribute from the Dongle tags. Obviously you aren't going to use a constant.  You would need to add the additional code to load your specific XML file.  :doh:

Edited by TheXman

Share this post


Link to post
Share on other sites
43 minutes ago, jswilcox1980 said:

Nine, I like this approach but i am unable to get to display the dongle number listed when i place the $anumber variable into my msgbox display code it returns a "1" only?  How do i get it to display the 10 digit dongle number?

It is an array.  So you need to provide the index of the var.  Like MsgBox (0,"",$aNumber[0] & "/" & $aNumber[1])

Share this post


Link to post
Share on other sites
3 hours ago, Nine said:

It is an array.  So you need to provide the index of the var.  Like MsgBox (0,"",$aNumber[0] & "/" & $aNumber[1])

certainly not looking for anyone to code this for me but i just dont get it.  obviously i am trying to do something beyond my knowledge level.  I am a visual learner so if i can see a working example i can understand what its doing and why.  I have tried adding your code and sort of understand what your saying but i dont know how to "provide the index" of the variable.

 

I Appreciate all of the help you both have given thus far.

Share this post


Link to post
Share on other sites

Did you run my example?  It works just fine.  Of course you have to make sure that you include the xml.au3 file from whatever location it resides in on your computer.

 

Edited by TheXman

Share this post


Link to post
Share on other sites

Show the code (including my part) that is not working, I will take a look, and help you correct it.

Share this post


Link to post
Share on other sites
9 hours ago, jswilcox1980 said:

... but i dont know how to "provide the index" of the variable.

Since it's Christmas time, and you characterize yourself as a visual learner, let us create a suitable metaphor ;).

You probably know the special Christmas calendars, where a surprise is waiting behind each of the 24 doors (often chocolate :lol:). Consider these doors a one-dimensional array.

However, there is a particular aspect that often irritates users. AutoIt uses so-called zero-based arrays (also termed 0-based). This means : "The array always starts at index zero. So, the first element in the array will be accessed by zero, the second element in the array is accessed at by one and so on. "

This indexing is different from how people would proceed in "normal life" (there, the first element would start with index 1). In AutoIt, it starts with Index [0] :

#include <Array.au3>
Local $aCalendarDoors [24] ; <== 24 Doors : UBound($aCalendarDoors)=Overall number of elements

For $i = 0 To UBound($aCalendarDoors) - 1 ; <== set all 24 elements - starting with index 0
    If $i < UBound($aCalendarDoors) - 1 Then
        $aCalendarDoors[$i] = "Door " & ($i + 1) ; <== +1 for the correct day (remember index 0)
    Else
        $aCalendarDoors[$i] = "Door " & ($i + 1) & " Happy Christmas :) "
    EndIf
Next
_ArrayDisplay($aCalendarDoors, "CalendarDoors", default, default, default, "Day")

But there is also an alternative notation :

The total number of elements is recorded in Index[0]. Index[1] to Index[24] represent the days. But this means, that we require 25 elements now :

#include <Array.au3>
Local $aCalendarDoors [25] ; <==  total number of elements PLUS 24 Doors

$aCalendarDoors [0] = 24 ; <==  set total number of elements
For $i = 1 To $aCalendarDoors[0] ; starting with index 1 TO number of elements
    If $i < 24 Then
        $aCalendarDoors[$i] = "Door " & $i
    Else
        $aCalendarDoors[$i] = "Door " & $i & " Happy Christmas :) "
    EndIf
Next
_ArrayDisplay($aCalendarDoors, "CalendarDoors", default, default, default, "Day")

@jswilcox1980 : I hope that has brought some light into the subject of arrays :lol:.

P.S. : to the experts : this is of course only a simplified description.


Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share this post


Link to post
Share on other sites
7 hours ago, Nine said:

Show the code (including my part) that is not working, I will take a look, and help you correct it.

This could be a formatting issue that i am missing.

#include <MsgBoxConstants.au3>
#include <file.au3>
#include <Array.au3>

FileVersions()
Func FileVersions()
    ; Retrieve the file versions of specified 3shape application files

    Local $DSver = FileGetVersion("C:\Program Files\3Shape\DentalManager\Dentalmanager.exe")
    Local $DSSver = FileGetVersion("C:\Program Files (x86)\3Shape\Dongle Server Service\DongleServer.exe")
    Local $ScnSvr = FileGetVersion("C:\Program Files\3Shape\Scansuite\Scanserver.exe")
    Local $SID = FileGetVersion("C:\Program Files\3Shape\ScanitDental\DentalDesktop.exe")
    Local $AppDsgnr = FileGetVersion("C:\Program Files\3Shape\ApplianceDesigner\ApplianceDesigner.exe")
    Local $Splintdsgnr = FileGetVersion("C:\Program Files\3Shape\SplintDesigner Splint\ApplianceDesigner.exe")

    Local $Versiontextfile = (@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt')

;Clears file from previous contents
        $hFile = FileOpen($Versiontextfile, 2)
        FileClose($hFile)

Opt ("MustDeclareVars", 1)

Local $aNumber = StringRegExp (FileRead ("C:\Program Files (x86)\3Shape\Dongle Server Service\DongleServerConfig.xml"),'Dongle Order="\d+" Number="(\d+)',$STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay ($aNumber)

; Display the file versions
    ;MsgBox (0,"test",$aNumber[0] & "/" & $aNumber[1])
    MsgBox($MB_SYSTEMMODAL,'3Shape Software Versions','Dental System:   ' & $DSver &@CRLF _
    & 'Dongle Server Service:   '& $DSSver & @CRLF _
    & 'ScanSuite:   '& $ScnSvr & @CRLF _
    & 'ScanitDental:   '& $SID & @CRLF _
    & 'Appliance Designer:   '& $AppDsgnr& @CRLF _
    & 'Splint Designer:   '& $SplintDsgnr &@CRLF _
    & 'Dongle Number:  '& $aNumber[0] & "/" & $aNumber[1] )

    MsgBox($MB_SYSTEMMODAL,'3Shape Software Versions', '       A summary has been saved on the desktop: ' & @CRLF & @CRLF& '" ' & @computername & ' 3shape Dental System File Versions.txt "')

;Below writes all data to text file on desktop
    FileWriteLine(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt',"")
    FileWriteLine(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt',"")
    FileWriteLine(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','***** 3SHAPE DENTAL SYSTEM FILE VERSIONS *****')
    FileWriteLine(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt',"")
    FileWriteLine(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt',"")
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','Dental System - '& $DSver)
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','Dongle Server Service - '& $DSSver)
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','ScanSuite - '& $ScnSvr)
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','Scanit Dental - '& $SID)
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','Appliance Designer - '& $AppDsgnr)
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','Splint Designer - '& $SplintDsgnr)
    FileWriteline(@DesktopDir &'\' & @computername & ' 3shape Dental System File Versions.txt','Dongle Number - ')

Run("notepad.exe " & $Versiontextfile, @WindowsDir, @SW_MAXIMIZE)
EndFunc

when i try to run it i get this:

"C:\Users\Jeremy Wilcox\Desktop\Test Scripts\Get 3shape File Versions.au3" (29) : ==> Subscript used on non-accessible variable.:


MsgBox($MB_SYSTEMMODAL,'3Shape Software Versions','Dental System:   ' & $DSver &@CRLF & 'Dongle Server Service:   '& $DSSver & @CRLF & 'ScanSuite:   '& $ScnSvr & @CRLF & 'ScanitDental:   '& $SID & @CRLF & 'Appliance Designer:   '& $AppDsgnr& @CRLF & 'Splint Designer:   '& $SplintDsgnr &@CRLF & 'Dongle Number:  '& $aNumber[0] & "/" & $aNumber[1] )
MsgBox($MB_SYSTEMMODAL,'3Shape Software Versions','Dental System:   ' & $DSver &@CRLF & 'Dongle Server Service:   '& $DSSver & @CRLF & 'ScanSuite:   '& $ScnSvr & @CRLF & 'ScanitDental:   '& $SID & @CRLF & 'Appliance Designer:   '& $AppDsgnr& @CRLF & 'Splint Designer:   '& $SplintDsgnr &@CRLF & 'Dongle Number:  '& $aNumber^ ERROR

 

Share this post


Link to post
Share on other sites

Ok, it means that the StringRegExp didn't work.  Either the xml file has been changed or the file doesn't exists.  When debugging you should put some ConsoleWrite after the problematic functions and show the @error and resulting flags. Here an example of what you should have done to solve your issue :

If Not FileExists ("C:\Program Files (x86)\3Shape\Dongle Server Service\DongleServerConfig.xml") Then Exit ConsoleWrite ("File is missing" & @CRLF)
Local $aNumber = StringRegExp (FileRead ("C:\Program Files (x86)\3Shape\Dongle Server Service\DongleServerConfig.xml"),'Dongle Order="\d+" Number="(\d+)',$STR_REGEXPARRAYGLOBALMATCH)
ConsoleWrite (@error & @CRLF)
If not IsArray ($aNumber) Then Exit ConsoleWrite ("SRE did not work" & @CRLF)
_ArrayDisplay ($aNumber)
MsgBox (0,"test",$aNumber[0] & "/" & $aNumber[1])

 

Share this post


Link to post
Share on other sites
;~ "some.xml" text: <?xml version="1.0" encoding="UTF-8"?><TServerConfig version="1"><Object type="TServerConfig" name="MainObject"><Property name="ServerPort" value="27027"/><Property name="ServerIP" value=""/><Property name="ExtendedLogging" value="False"/></Object><Dongles><Dongle Order="0" Number="0123456789"/><Dongle Order="1" Number="1122334455"/></Dongles></TServerConfig>
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.Load("some.xml")

$oDongles = $oXML.selectsinglenode("//Dongles").childnodes
For $oDongle In $oDongles
    $sOrder = $oDongle.getattribute("Order")
    $sNumber = $oDongle.getattribute("Number")
    ConsoleWrite("Order=[" & $sOrder & "], Number=[" & $sNumber & "]" & @CRLF)
Next

Output:

Order=[0], Number=[0123456789]
Order=[1], Number=[1122334455]

 

You can, in the loop, add the data to a 2d array.

 

Example:

#include <Array.au3>

;~ "some.xml" text: <?xml version="1.0" encoding="UTF-8"?><TServerConfig version="1"><Object type="TServerConfig" name="MainObject"><Property name="ServerPort" value="27027"/><Property name="ServerIP" value=""/><Property name="ExtendedLogging" value="False"/></Object><Dongles><Dongle Order="0" Number="0123456789"/><Dongle Order="1" Number="1122334455"/></Dongles></TServerConfig>
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.Load("some.xml")

Enum $iDongles_Order, $iDongles_Number, $iDongles_Ubound
Local $aDongles[0][$iDongles_Ubound]

$oDongles = $oXML.selectsinglenode("//Dongles").childnodes
For $oDongle In $oDongles
    ReDim $aDongles[UBound($aDongles)+1][$iDongles_Ubound]
    $sOrder = $oDongle.getattribute("Order")
    $sNumber = $oDongle.getattribute("Number")
    $aDongles[UBound($aDongles)-1][$iDongles_Order]=$sOrder
    $aDongles[UBound($aDongles)-1][$iDongles_Number]=$sNumber
    ConsoleWrite("Order=[" & $sOrder & "], Number=[" & $sNumber & "]" & @CRLF)
Next
_ArrayDisplay($aDongles)

array output:

0|0123456789
1|1122334455
 

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...