我正在解析从Web API返回的一段XML。我正在寻找一个特定的节点。如果该节点不存在,则根据MSXML documentation,它返回null。
问题是,我不知道如何在AutoIT中检查null。我已阅读online API doc for Null,但是当我使用AutoIt3Wrapper v.2.1.2.9运行脚本时,它无法识别null。
这是一个展示我的意思的示例脚本:
$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
答案 0 :(得分:0)
我找到了解决问题的快速解决方法。
通过使用ObjName()
,我可以检查返回的COM对象的名称,如果成功则为IXMLDOMElement
:
If ObjName($node) = "IXMLDOMElement" Then
MsgBox(0, "", "Success")
Else
MsgBox(0, "", "Failure")
EndIf
答案 1 :(得分:0)
您可以使用IsObj()
来测试是否返回了有效对象:
If Not IsObj($oNode) Then
MsgBox(0, 'ERROR', 'Node is invalid!')
EndIf