0

附加的 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>
4

1 回答 1

0

true如果 XPath存在,则以下代码将打印/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 最初有错误。tag 没有结束标签<SignonRs>。在运行代码之前修复它。

于 2019-01-29T17:29:34.733 回答