【问题标题】:Parse XML Soap Response with vbscript使用 vbscript 解析 XML Soap 响应
【发布时间】:2018-10-13 19:34:03
【问题描述】:

我正在向第三方 API 发送一个 XML 请求,我收到了一个响应,我需要对其进行解析以获取错误消息或获取成功和订单 ID。

这是我从错误消息中得到的响应。

<!--?xml version="1.0" encoding="UTF-8"?-->
<soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:body>
      <createbackgroundcheckresponse xmlns="https://www.website.com/sif/ws/hrxml/2.0">
         <applicationacknowledgement>
            <payloadresponsesummary>
            <payloaddisposition>
               <entitydisposition>
                  <entityinstancexpath>/Order/SearchPackage[1]/ReferenceId[1]/IdValue[1] </entityinstancexpath>
                  <entityexception>
                     <exception>
                        <exceptionidentifier>02x0033</exceptionidentifier>
                        <exceptionseverity>Fatal</exceptionseverity>
                        <exceptionmessage>An order is already being processed with the same user</exceptionmessage>
                     </exception>
                  </entityexception>
               </entitydisposition>
            </payloaddisposition>
         </payloadresponsesummary></applicationacknowledgement>
      </createbackgroundcheckresponse>
   </soap:body>
</soap:envelope>

我能够获得响应并将其写入浏览器,但无法解析它。这是我发送时的内容。

Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP")

xmlHttp.Open "POST", xUrl, False         'False = Synckronous Process
xmlHttp.SetRequestHeader "Content-type", contentType
xmlHttp.SetRequestHeader "Content-Length", Len(xmlBody)

xmlHttp.SetOption 2, 13056          'Ignore SSL errors
xmlHttp.Send xmlBody

'Get the response back
sRet = xmlHttp.ResponseText

If xmlHttp.ReadyState = 4 And xmlHttp.Status = 200 Then
    HttpPOST = sRet
Else
    HttpPOST = "[Http-Failure] " & vbNewLine & sRet
End If

一旦我把数据拿回来,这就是我所拥有的。

Set xmlDoc = CreateObject("Msxml2.DOMDocument.3.0")

xmlDoc.Async = False
xmlDoc.ResolveExternals = False
xmlDoc.SetProperty "ServerHTTPRequest", True
xmlDoc.ValidateOnParse = True
xmlDoc.PreserveWhiteSpace = True

If xmlDoc.LoadXML(sRet) Then
    'Dim xmlNodeList
    Set xmlNodeList = xmlDoc.GetElementsByTagName("exceptionmessage")
    If Not (xmlNodeList Is Nothing) Then
        response.Write "exceptionmessage is Not Nothing<P>"
        If xmlNodeList.Length > 0 Then
            response.Write "xmlNodeList length > 0<P>"
            Response.Write "exceptionmessage Value is : " & xmlNodeList(0).Text & vbNewLine & "<BR>"
        Else
            Response.Write "exceptionmessage length is 0<P>"
        End If
    Else
        response.Write "exceptionmessage is Nothing<P>"
    End If

    Set nodeXML = Nothing
End If

当我运行它时,它将通过xmlDoc.LoadXML()。然后它将通过If Not (xmlNodeList is Nothing) Then 的第一次检查。但是它的长度是0,所以它不能通过那部分。

【问题讨论】:

  • VBScript 的 XML 解析器在遇到错误时会静默失败。检查xmlDoc.ParseError 的值。如果结果不为 0,请检查 xmlDoc.ParseError.Reason 的值。
  • VBScript 没有“XML 解析器”。您似乎在谈论 MSXML v. 3 DOM,它是 Windows 的一部分,而不是 VBScript,它不是解析器,而是 DOM。在 VBScript 中处理 SOAP 的首选方法是使用已弃用的 SOAP Toolkit 3.0,它在比通用 XML 处理更高的级别上运行。您还可以考虑使用 3rd 方 PocketSOAP 库。

标签: xml vbscript xml-parsing asp-classic msxml


【解决方案1】:

这里发生了一些事情。

1) 通过查看 LoadXML 的返回码来检查 XML 解析器的错误处理,然后检查 parseError 对象是否返回码为 false 以找出问题所在。

2) SOAP XML 使用名称空间(恕我直言,这是不必要的),使用名称空间进行查询我使用 XPath (SelectionLanguage=XPath),然后将 SelectionNameSpaces 设置为 URL。您的 XML 使用“默认”命名空间,但要使用它进行查询,您需要为其命名,我选择了“ns”,但它可以是任何名称。最后,使用 selectNodes 或 selectSingleNode 查询和检查结果。

  Dim xml As Object

  Set xml = CreateObject("MSXML2.DOMDocument")

  Dim bReturn As Boolean
  bReturn = xml.Load("c:\\temp\testsoap.xml")
  If (bReturn = False) Then
    MsgBox ("Error loading XML Response: " + CStr(xml.parseError.errorCode) + " " + xml.parseError.reason)
  End If

  Call xml.setProperty("SelectionLanguage", "XPath")
  Call xml.setProperty("SelectionNamespaces", "xmlns:ns='https://www.website.com/sif/ws/hrxml/2.0'")

  Dim ndException As Object
  Set ndException = xml.selectSingleNode("//ns:exception")
  If (ndException Is Nothing) Then
    ' it worked
  Else
    MsgBox ("Exception found in XML: " + ndException.Text)
  End If

【讨论】:

    猜你喜欢
    • 2020-02-07
    • 1970-01-01
    • 2021-06-07
    • 2016-09-03
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多