简体   繁体   English

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

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

Attached XML sample, In the attached XML I want to verify the tag Elements presence for ex: PayloadList/IFXResp/IFX/GeneralStatus/StatusCode 附加的XML示例,在附加的XML中,我想验证标签元素是否存在,例如:PayloadList / IFXResp / IFX / GeneralStatus / StatusCode

it would be great if anyone can help me to get the above xpath code. 如果有人可以帮助我获得上面的xpath代码,那就太好了。 Looking for code in vbscript/UFT to print the Tag Elements Name 在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>

The following code will print true if the XPath /PayloadList/Payload/IFXResp/IFX/GeneralStatus/StatusCode exists, else it returns false. 如果XPath /PayloadList/Payload/IFXResp/IFX/GeneralStatus/StatusCode存在,则以下代码将显示true ,否则返回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

You can also use selectSingleNode method instead of selectNodes method as shown below inside the function: 您还可以使用selectSingleNode方法而不是selectNodes方法,如下图所示:

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

Note: Your XML initially had errors. 注意:您的XML最初有错误。 There is no closing tag for the tag <SignonRs> . 标签<SignonRs>没有结束标签。 Fix it before running the code. 在运行代码之前对其进行修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM