Jump to content

XML Check if a node exists.


Recommended Posts

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

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

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

 

Link to comment
Share on other sites

@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 by Nine
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...