Vbscript/UFT如何获取标签元素名称来验证标签元素的Xpath

Vbscript/UFT how to get the Tag elements name to validate Xpath of tag elements

附XML示例,在附XML我要验证标签元素存在 例如:PayloadList/IFXResp/IFX/GeneralStatus/StatusCode

如果有人能帮我得到上面的xpath代码就太好了。在 vbscript/UFT 中寻找代码以打印标记元素名称

<?xmlversion="1.0" encoding="UTF-8"?>
<PayloadList>
<Payload>
  <IFXResp>
     <IFX>
        <GeneralStatus>
           <StatusCode>0</StatusCode>
           <StatusLevel>IFX</StatusLevel>
           <StatusDesc>The request was processed successfully.</StatusDesc>
        </GeneralStatus>
        <Status>
           <StatusCode>0</StatusCode>
           <Severity>Info</Severity>
           <StatusDesc>The request was processed successfully.</StatusDesc>
           <SupportUID>DD2B1DFF-57657657-6767-8013-C9787878AF00</SupportUID>
        </Status>
        <SignonRs>
     </IFX>
  </IFXResp>
</Payload>
</PayloadList>

如果 XPath /PayloadList/Payload/IFXResp/IFX/GeneralStatus/StatusCode 存在,下面的代码将打印 true,否则 returns false。

Option Explicit
Dim strXMLFilePath, strXPath
strXMLFilePath = "F:\test.xml"
strXPath = "/PayloadList/Payload/IFXResp/IFX/GeneralStatus/StatusCode"
MsgBox fn_readXML(strXMLFilePath,strXPath)

Function fn_readXML(strXMLFilePath, strXPath)
    Dim objXML, objNodes
    Set objXML = CreateObject("MSXML2.DomDocument")
    objXML.async= False
    objXML.load strXMLFilePath
    With objXML.parseError
        If .errorCode = 0 Then
            Set objNodes = objXML.selectNodes(strXPath)
            If objNodes.length > 0 Then
                fn_readXML = True
            Else
                fn_readXML = false
            End If
        Else
            MsgBox "Cannot parse the XML File!!!" & vbCrLf &_
                   "Error Code: " & .errorCode & vbCrLf &_
                   "Reason: " & .reason & vbCrLf &_
                   "Line: " & .line
        End If
    End With
    Set objXML = Nothing
End Function

您也可以在函数内部使用 selectSingleNode 方法代替 selectNodes 方法,如下所示:

Dim objNode
Set objNode = objXML.selectSingleNode(strXPath)
If objNode Is Nothing Then
    fn_readXML = False
Else
    fn_readXML = True
End If

注意:您的XML最初有错误。标签 <SignonRs> 没有结束标签。在 运行 代码之前修复它。