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

时间:2019-01-29 09:28:05

标签: xml vbscript xml-parsing uft14 html-xml-utils

附加的XML示例,在附加的XML中,我要验证标签Elements的存在 例如:PayloadList / IFXResp / IFX / GeneralStatus / StatusCode

如果有人可以帮助我获得上面的xpath代码,那就太好了。在vbscript / UFT中查找代码以打印标记元素名称

DbContext

1 个答案:

答案 0 :(得分:0)

如果XPath true存在,则以下代码将打印/PayloadList/Payload/IFXResp/IFX/GeneralStatus/StatusCode,否则返回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>没有结束标签。在运行代码之前对其进行修复。