简体   繁体   中英

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

it would be great if anyone can help me to get the above xpath code. Looking for code in vbscript/UFT to print the Tag Elements Name

<?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.

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:

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. There is no closing tag for the tag <SignonRs> . Fix it before running the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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