Question

I am parsing a piece of XML returned from a Web API. I am looking for a particular node. If that node does not exist, according to the MSXML documentation, it returns null.

The problem is, I don't know how to check for null in AutoIT. I have read the online API doc for Null, but when I run the script using AutoIt3Wrapper v.2.1.2.9, it does not recognize null.

Here is a sample script to show what I mean:

$oXMLDOM = ObjCreate("Msxml2.DOMDocument.3.0")
$xml = '<response><error code="1"><![CDATA[ Incorrect password or username ]]></error></response>'
$oXMLDOM.loadXML($xml)
$node = $oXMLDOM.selectSingleNode("/response/error")
MsgBox(0, "", $node.text) ;; No problems
$node = $oXMLDOM.selectSingleNode("/response/token")
;; $node should be 'null' now; how do I check that in AutoIT?
MsgBox(0, "", $node.text) ;; Fails horribly
Was it helpful?

Solution

You could use IsObj() to test if a valid object was returned:

If Not IsObj($oNode) Then
    MsgBox(0, 'ERROR', 'Node is invalid!')
EndIf

OTHER TIPS

I have kind of found a quick workaround for my problem.

By using ObjName(), I can check the name of the COM object returned, which is IXMLDOMElement if it is successful:

If ObjName($node) = "IXMLDOMElement" Then
    MsgBox(0, "", "Success")
Else
    MsgBox(0, "", "Failure")
EndIf
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top