Jump to content

Recommended Posts

Posted

I have created an app that allows me to save server names as field values and other information associated with their nodes. (example below)

<Servers>
    <ServerInfo>
        <Server_Name>MyServerName</Server_Name>
        <User>user_name</User>
        <Password>0x8534C1E508D4CF29AC17</Password>
    </ServerInfo>
</Servers

I am trying to use "_XMLNodeExists" from "_XMLDOMWrapper.au3" to search for existing server entries by field value. (Sorry if my XML terms are not quite right) I didn't quite follow how to search and I am not sure if this function can perform how I expect it to...

My attempt 

_XMLNodeExists('//Servers/ServerInfo/Server_Name/MyServerName')

Any suggestions how I might go about searching my XML file? I had thought of just searching as plain text.

Posted
; #FUNCTION# ===================================================================
; Name ..........: _XMLNodeExists
; Description ...: Checks for the existence of a node or nodes matching the specified path
; Syntax.........: _XMLNodeExists($strXPath)
; Parameters ....: $strXPath      - Path to check for.
; Return values .: Success        - 1 or Higher , 0
;                  Failure        - 0 and @Error set to:
;                  |0 - No error.
;                  |1 - No XML object @extended = 31.
;                  |2 - Node not found.
; Author ........: Stephen Podhajecki <gehossafats a t netmdc.com>
; Modified ......:
; Remarks .......: Returns the number of nodes found (could be greater than 1)
; Related .......:
; Link ..........;
; Example .......; [yes/no]
; ==============================================================================
Func _XMLNodeExists(ByRef $objDoc, $strXPath)
    If Not IsObj($objDoc) Then
        _XMLError("No object passed to function _XMLNodeExists")
        Return SetError(1, 31, 0)
    EndIf
    Local $iCount
    Local $objNode = $objDoc.SelectNodes($strXPath)
    If IsObj($objNode) Then $iCount = $objNode.length
    $objNode = 0
    If $iCount Then Return $iCount
    Return SetError(2, 0, 0)
EndFunc   ;==>_XMLNodeExists

 

This header is not complete.

when you focus on function declaration:

.....
Func _XMLNodeExists(ByRef $objDoc, $strXPath)
.....

Then you find that you miss one parameter ;)

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Interesting... your version of "_XMLNodeExists" appears to be different than what I have...

; #FUNCTION# ===================================================================
; Name ..........: _XMLNodeExists
; Description ...: Checks for the existence of a node or nodes matching the specified path
; Syntax.........:  _XMLNodeExists($strXPath)
; Parameters ....: $strXPath      - Path to check for.
; Return values .: Success        - 1 or Higher , 0
;                  Failure        - 0 and @Error set to:
;                  |0 - No error.
;                  |1 - No XML object @extended = 31.
;                  |2 - Node not found.
; Author ........: Stephen Podhajecki <gehossafats a t netmdc.com>
; Modified ......:
; Remarks .......: Returns the number of nodes found (could be greater than 1)
; Related .......:
; Link ..........;
; Example .......; [yes/no]
; ==============================================================================
Func _XMLNodeExists($strXPath)
    If Not IsObj($objDoc) Then
        _XMLError("No object passed to function _XMLNodeExists")
        Return SetError(1, 31, 0)
    EndIf
    Local $objNode, $iCount
    Local $objNode = $objDoc.SelectNodes($strXPath)
    If IsObj($objNode) Then $iCount = $objNode.length
    $objNode = 0
    If $iCount Then Return $iCount
    Return SetError(2, 0, 0)
EndFunc   ;==>_XMLNodeExists

If I create a test script that only looks for the node name, it returns the number of matching nodes. My test script:

#include <_XMLDOMWrapper.au3>

$searchVal = 'MyServer'
_XMLFileOpen(@ScriptDir&"\Servers.xml")

$srchRes = _XMLNodeExists('//Servers/ServerInfo/Server_Name')

ConsoleWrite("Results: "&$srchRes&@CRLF)

This seems to return similar results as "_XMLGetNodeCount". I am looking for field value search results.

Posted (edited)
  On 1/6/2016 at 9:17 PM, gritts said:

Interesting... your version of "_XMLNodeExists" appears to be different than what I have...

I take this function snippet from: 

Global Const $_XMLUDFVER = "1.0.3.98"

But you can try to look here: XMLWrapperEx.au3 - BETA 

  On 1/6/2016 at 9:17 PM, gritts said:

If I create a test script that only looks for the node name, it returns the number of matching nodes. My test script:

#include <_XMLDOMWrapper.au3>

$searchVal = 'MyServer'
_XMLFileOpen(@ScriptDir&"\Servers.xml")

$srchRes = _XMLNodeExists('//Servers/ServerInfo/Server_Name')

ConsoleWrite("Results: "&$srchRes&@CRLF)

This seems to return similar results as "_XMLGetNodeCount". I am looking for field value search results.

This is correct, as in this element:
 

<Server_Name>MyServerName</Server_Name>

"MyServerName" is value so you can not check like this:

_XMLNodeExists('//Servers/ServerInfo/Server_Name/MyServerName')

as there is no element MyServerName. 

EDIT: 

If you want to get value then you should use _XMLGetValue(...)

 

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Thank you for your replies @mLipok, @jdelaney. Your suggestions gave me and an idea and reminded me of some steps I had taken in the past. Below is my initial groundwork for my solution that may be useful for others. If there are suggestions for improving my methods, let me know. :)

#include <_XMLDOMWrapper.au3>
#include <array.au3>

Local $aSrvInf = _ReadXMLFile(), $tmp

$tmp = _ArraySearch($aSrvInf,"MyServerName",0,0,0,0,1,0) ;Search for server name in column designated for server names
ConsoleWrite("Found match at "&$tmp&@CRLF) ;Show the row number the match was found at


Func _ReadXMLFile() ;Create an array with the values stored in the server information XML
Local $nodCnt, $c
If Not FileExists(@ScriptDir&"\servers.xml") Then
    ConsoleWrite("Unable to open servers.xml. Please be sure it is in the same directory as the script."&@CRLF)
    Exit
EndIf

If not _XMLFileOpen(@ScriptDir&"\servers.xml") Then
    ConsoleWrite("Error opening the 'servers.xml' file")
Else
    $nodCnt = _XMLGetNodeCount("//ServerInfo")
    If $nodCnt = -1 Then
        ConsoleWrite("There was a problem reading the servers.xml file. Please check the file."&@CRLF)
        Exit
    EndIf
    ConsoleWrite("There are "&$nodCnt&" ServerInfo nodes"&@CRLF)
    Local $aServInfo[$nodCnt][3]
    For $c = 1 to $nodCnt
        $aServInfo[$c-1][0] = _XMLGetChildText('//ServerInfo['&$c&']/Server_Name')[1]
        $aServInfo[$c-1][1] = _XMLGetChildText('//ServerInfo['&$c&']/User')[1]
        $aServInfo[$c-1][2] = _XMLGetChildText('//ServerInfo['&$c&']/Password')[1]
    Next
    _ArrayDisplay($aServInfo)
    Return($aServInfo)
EndIf

EndFunc

This searches and XML file with this format:

<Servers>
    <ServerInfo>
        <Server_Name>MyServerName</Server_Name>
        <User>user_name</User>
        <Password>0x8534C1E508D4CF29AC17</Password>
    </ServerInfo>
</Servers>

 

Posted

I'd go this route:

#include <Array.au3>
$xml = "<Servers>" & _
"<ServerInfo><Server_Name>MyServerName1</Server_Name><User>user_name1</User><Password>0x8534C1E508D4CF29AC17a</Password></ServerInfo>" & _
"<ServerInfo><Server_Name>MyServerName2</Server_Name><User>user_name2</User><Password>0x8534C1E508D4CF29AC17b</Password></ServerInfo>" & _
"<ServerInfo><Server_Name>MyServerName3</Server_Name><User>user_name3</User><Password>0x8534C1E508D4CF29AC17b</Password></ServerInfo>" & _
"</Servers>"
Global Enum $iServerName, $iUser, $iPassword, $iUBound



Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.LoadXML($xml)
$oServers = $oXML.selectNodes("//ServerInfo")
Local $a
For $oServer In $oServers

    If IsArray($a) Then
        ReDim $a[UBound($a)+1][$iUBound]
    Else
        Local $a[1][$iUBound]
    EndIf

    $a[UBound($a)-1][$iServerName] = $oServer.selectSingleNode("./Server_Name").text
    $a[UBound($a)-1][$iUser] = $oServer.selectSingleNode("./User").text
    $a[UBound($a)-1][$iPassword] = $oServer.selectSingleNode("./Password").text
Next
_ArrayDisplay($a)

 

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.
  • 3 weeks later...
Posted

@jdelaney, thank you for your reply.... I like your approach of using enumerated variables. I hadn't thought of using them in the past. I am wondering how your ObjCreate("Microsoft.XMLDOM") acts different that that within the _XMLDOMWrapper.au3 UDF?

Posted

You can check it like this:

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

Local $oObj1 = ObjCreate("Microsoft.XMLDOM")
ObjName_FlagsValue($oObj1)

Local $oObj2 = ObjCreate("Msxml2.DOMDocument.3.0")
ObjName_FlagsValue($oObj2)

Func ObjName_FlagsValue(ByRef $oObj)
    Local $sInfo = ''

    $sInfo &= '+>' & @TAB & 'ObjName($oObj,1) {The name of the Object} =' & @CRLF & @TAB & ObjName($oObj, $OBJ_NAME) & @CRLF

    ; HELPFILE REMARKS: Not all Objects support flags 2 to 7. Always test for @error in these cases.
    $sInfo &= '+>' & @TAB & 'ObjName($oObj,2) {Description string of the Object} =' & @CRLF & @TAB & ObjName($oObj, $OBJ_STRING)
    If @error Then $sInfo &= '@error = ' & @error
    $sInfo &= @CRLF & @CRLF

    $sInfo &= '+>' & @TAB & 'ObjName($oObj,3) {The ProgID of the Object} =' & @CRLF & @TAB & ObjName($oObj, $OBJ_PROGID)
    If @error Then $sInfo &= '@error = ' & @error
    $sInfo &= @CRLF & @CRLF

    $sInfo &= '+>' & @TAB & 'ObjName($oObj,4) {The file that is associated with the object in the Registry} =' & @CRLF & @TAB & ObjName($oObj, $OBJ_FILE)
    If @error Then $sInfo &= '@error = ' & @error
    $sInfo &= @CRLF & @CRLF

    $sInfo &= '+>' & @TAB & 'ObjName($oObj,5) {Module name in which the object runs (WIN XP And above). Marshaller for non-inproc objects.} =' & @CRLF & @TAB & ObjName($oObj, $OBJ_MODULE)
    If @error Then $sInfo &= '@error = ' & @error
    $sInfo &= @CRLF & @CRLF

    $sInfo &= '+>' & @TAB & 'ObjName($oObj,6) {CLSID of the object''s coclass} =' & @CRLF & @TAB & ObjName($oObj, $OBJ_CLSID)
    If @error Then $sInfo &= '@error = ' & @error
    $sInfo &= @CRLF & @CRLF

    $sInfo &= '+>' & @TAB & 'ObjName($oObj,7) {IID of the object''s interface} =' & @CRLF & @TAB & ObjName($oObj, $OBJ_IID)
    If @error Then $sInfo &= '@error = ' & @error
    $sInfo &= @CRLF & @CRLF

    MsgBox($MB_SYSTEMMODAL, "ObjName:", $sInfo)
EndFunc   ;==>ObjName_FlagsValue

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

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
×
×
  • Create New...