AutoitMike Posted April 26, 2022 Share Posted April 26, 2022 I looked for information how how to test that a node exists on this forum and found the following: #include <_XMLDomWrapper.au3> $sXMLFile = "C:\XMLtest.xml" $result = _XMLFileOpen($sXMLFile) If $result = 0 Then MsgBox(0,"", "Error opening " & $sXMLFile) Exit EndIf $sXPath = "//Parent/Child" $nodeArray = _XMLSelectNodes($sXPath) if $nodeArray[0] > 0 Then MsgBox(0,"", "Service node found") ;MsgBox(0,"",$nodeArray [0]) Else MsgBox(0,"", "Service node NOT found") EndIf This in fact works, however, I have an extensive application already running reading / changing and cloning nodes using a different way of opening and using an XML file: #include <_XMLDomWrapper.au3> Local $oXML = ObjCreate("Microsoft.XMLDOM") $oXML.load("C:\test.xml") ;IF...this Xpath exists, then a message box appears with the number of nodes. ;IF... this xpath does not exist, then an error is produced, "0" is not returned. MsgBox(0,"","Count = " & $oXML.SelectNodes('/Parent/Child').length) ;Then when I am done doing the rest of my modifications, I use this to save the file: $oXML.save("C:\test.xml") You would think that this would work, but it does not: $NodeArray=$oXML.SelectNodes("/parent/Child") VarGetType reports that $Nodearray is an object when there is no such Xpath. I cant find anything about an Array function in the MS documentation. I would like to test for non existent nodes without re inventing the wheel. Thanks in advance Link to comment Share on other sites More sharing options...
Danp2 Posted April 26, 2022 Share Posted April 26, 2022 Not sure that I understand the issue. Can't you just check the object's length property to verify if the node was found? $NodeArray=$oXML.SelectNodes("/parent/Child") If $NodeArray.length Then ; do more processing here EndIf Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
AutoitMike Posted April 27, 2022 Author Share Posted April 27, 2022 The problem occurs when the checked node does not exist. That is what I want to test for. If the node does not exist, then Autoit crashes, an error is raised and the program stops. In the first example _XMLSelectNodes("xpath") creates an array whether or not any node exists. If no node exists, an array is created, however, with zero elements. If I insert the line in my program $nodeArray = _XMLSelectNodes($sXPath) It fails, $NodeArray is not created. Link to comment Share on other sites More sharing options...
Danp2 Posted April 27, 2022 Share Posted April 27, 2022 It works as expected In my tests. For example, the following uses the same file you provided in an earlier thread -- Local $oXML = ObjCreate("Microsoft.XMLDOM") $oXML.load("CloneRPT.hr4") testXpath("//ReportInfo/Service") ; exists testXpath("//ReportInfo/noService") ; non-existent Func testXpath($sXpath) $NodeArray=$oXML.SelectNodes($sXpath) MsgBox(0, $sXpath,"VarGetType = " & VarGetType($NodeArray) & @CRLF & "Count = " & $NodeArray.length) EndFunc Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Nine Posted April 27, 2022 Share Posted April 27, 2022 (edited) @Danp2 I believe OP has an old version of the COM XML object. In a previous thread, he was struggling too with boolean parameter and he needed to send it in string instead of a real boolean. This must be another collateral issue of its old version. @AutoitMike Try using IsArray function Edited April 27, 2022 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Danp2 Posted April 27, 2022 Share Posted April 27, 2022 @NineThanks for jumping in on this thread. I saw that prior post and I suspected the same thing about different versions of the XML object. @AutoitMikeI'm guessing that you are using an older OS (ie: Windows 7). Are you able to test on Win10 or 11? Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
AutoitMike Posted April 28, 2022 Author Share Posted April 28, 2022 I'm using Win7. I'll try "IsArray" Link to comment Share on other sites More sharing options...
AutoitMike Posted April 28, 2022 Author Share Posted April 28, 2022 Holy cow, Figured it out. Local $oXML = ObjCreate("Microsoft.XMLDOM") $oXML.Load("C:\File.xml") ;Xpath Exists--$N > 0 $N=$oXML.SelectNodes("//Parent/Child").length ;Xpath does not Exist--$N =0 $N=$oXML.SelectNodes("//Parent/yyChildxx").length I just realized that I needed to add ".length" $N is never an array. Thanks again---- Danp2 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now